From: Tom Lane <tgl@sss.pgh.pa.us>
To: code@phaedrusdeinus.org
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #6381: Incorrect greediness behavior in certain regular expressions
Date: Fri, 06 Jan 2012 02:21:19 -0500
Message-ID: <3498.1325834479@sss.pgh.pa.us> (raw)
In-Reply-To: <E1RixjF-0006gP-Dt@wrigleys.postgresql.org>
References: <E1RixjF-0006gP-Dt@wrigleys.postgresql.org>
code@phaedrusdeinus.org writes:
> This, more complex/complete version, matches greedily, which is incorrect:
> =# select regexp_matches('http://blahblah.com/asdf';,
> 'http(s?)(:|%3a)(//|%2f%2f)(.*?)(/|%2f|$)');
> regexp_matches
> --------------------------------
> {"",:,//,blahblah.com/asdf,""}
I do not believe this is a bug; the RE code appears to me to be
following its specification, as per the detailed rules in section
9.7.3.5:
http://www.postgresql.org/docs/9.1/static/functions-matching.html#POSIX-MATCHING-RULES
Specifically, the regex as a whole is considered greedy because the
first subpart with a greediness attribute is the (s?) piece, and the ?
quantifier is greedy. Therefore, the regex as a whole matches the
longest possible string, which in this case will be up to the end of the
input. It's true that the (.*?) subpart is non-greedy, but that only
affects how much of the overall match length can get assigned to that
subpart relative to other subparts, and in this case there is no
flexibility to assign it more or less of the match once the total match
length is determined.
The reason your second case "works" as you expect is that there's no way
for it to match anything beyond the last slash:
> select regexp_matches('http://blahblah.com/asdf';,
> 'http(s?)(:|%3a)(//|%2f%2f)(.*?)(/|%2f)');
> regexp_matches
> --------------------------
> {"",:,//,blahblah.com,/}
There's only one possible match here, independently of whether the (.*?)
portion is greedy or not.
With this example, you could get the results you're after by using
the non-greedy ?? operator for the first subpart:
regression=# select regexp_matches('http://blahblah.com/asdf';, 'http(s??)(:|%3a)(//|%2f%2f)(.*?)(/|%2f|$)');
regexp_matches
--------------------------
{"",:,//,blahblah.com,/}
although I cannot tell whether that generalizes to your real problem.
regards, tom lane
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, code@phaedrusdeinus.org
Subject: Re: BUG #6381: Incorrect greediness behavior in certain regular expressions
In-Reply-To: <3498.1325834479@sss.pgh.pa.us>
References: <E1RixjF-0006gP-Dt@wrigleys.postgresql.org> <3498.1325834479@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