pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: depesz@depesz.com
Cc: kosiodg@yahoo.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17761: Questionable regular expression behavior
Date: Fri, 27 Jan 2023 11:04:53 -0500
Message-ID: <3334493.1674835493@sss.pgh.pa.us> (raw)
In-Reply-To: <Y9PGupnpVoN/uQ2w@depesz.com>
References: <17761-5a78c34533f3ab72@postgresql.org>
	<Y9PGupnpVoN/uQ2w@depesz.com>

hubert depesz lubaczewski <depesz@depesz.com> writes:
> On Fri, Jan 27, 2023 at 09:27:35AM +0000, PG Bug reporting form wrote:
>> Executing:
>> select regexp_matches('a 1x1250x2500',
>> '(a).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?');
>> returns: {a,1,1,NULL}
>> while executing:
>> select regexp_matches('a 1x1250x2500',
>> '(a|b).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?');
>> returns: {a,1,1250,2500}
>> 
>> Shouldn't both results be equal?

> The problem is, afair, that there is some state in pg's regexp engine
> that makes greedy/ungreedy decision once per regexp.

Yeah.  Without having traced through it, I'm fairly sure that in the
first case, we have "(a)" which has no greediness, then ".*?" which
is non-greedy, and then that determines the overall greediness as
non-greedy, so it goes for the shortest overall match not the longest.

In the second case, "(a|b)" is greedy because anything involving "|"
is greedy, so we immediately decide we'll be greedy overall.

The fine manual explains how you can force greediness or non-greediness
when the engine's default rules for that don't do what you want.

			regards, tom lane





view thread (4+ messages)  latest in thread

Message-ID: <3334493.1674835493@sss.pgh.pa.us>
Permalink:  ../3334493.1674835493@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3334493.1674835493@sss.pgh.pa.us

 · 

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, depesz@depesz.com, kosiodg@yahoo.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17761: Questionable regular expression behavior
  In-Reply-To: <3334493.1674835493@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