From: Tom Lane <tgl@sss.pgh.pa.us>
To: christian_maechler@hotmail.com
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13538: REGEX non-greedy is working incorrectly (and also greedy matches fail if non-greedy is present)
Date: Tue, 04 Aug 2015 11:39:46 -0400
Message-ID: <32324.1438702786@sss.pgh.pa.us> (raw)
In-Reply-To: <26003.1438700337@sss.pgh.pa.us>
References: <20150803224450.1115.8803@wrigleys.postgresql.org>
<CAKFQuwaXQvXx9OEDNXAA5eXdbP0CKBnWow+L0aoh1vH2J6LrtQ@mail.gmail.com>
<DUB128-W2990BB7605238976ADE6AEF8760@phx.gbl>
<26003.1438700337@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
I wrote:
> As David says, these examples appear to be following what's stated in
> http://www.postgresql.org/docs/9.4/static/functions-matching.html#POSIX-MATCHING-RULES
> The Spencer regex engine we use has a notion of greediness or
> non-greediness of the entire regex, and further that that takes precedence
> for determining the overall match length over greediness of individual
> subexpressions. That behavior might be inconvenient for this particular
> use-case, but that doesn't make it a bug.
BTW, perhaps it would be worth adding an example to that section that
shows how to control this behavior. The trick is obvious once you've seen
it, but not so much otherwise: you add something to the start of the regex
that establishes the overall greediness you want, but can never actually
match any characters. "\0*" or "\0*?" will work fine in Postgres
use-cases since there can never be a NUL character in the data.
regression=# select regexp_matches('abc01234xyz', '(.*)(\d+)(.*)');
regexp_matches
-----------------
{abc0123,4,xyz}
(1 row)
regression=# select regexp_matches('abc01234xyz', '(.*?)(\d+)(.*)');
regexp_matches
----------------
{abc,0,""}
(1 row)
regression=# select regexp_matches('abc01234xyz', '\0*(.*?)(\d+)(.*)');
regexp_matches
-----------------
{abc,01234,xyz}
(1 row)
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, christian_maechler@hotmail.com, david.g.johnston@gmail.com
Subject: Re: BUG #13538: REGEX non-greedy is working incorrectly (and also greedy matches fail if non-greedy is present)
In-Reply-To: <32324.1438702786@sss.pgh.pa.us>
References: <20150803224450.1115.8803@wrigleys.postgresql.org> <CAKFQuwaXQvXx9OEDNXAA5eXdbP0CKBnWow+L0aoh1vH2J6LrtQ@mail.gmail.com> <DUB128-W2990BB7605238976ADE6AEF8760@phx.gbl> <26003.1438700337@sss.pgh.pa.us> <32324.1438702786@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox