Re: BUG #17761: Questionable regular expression behavior

From: Kosio Dimitrov <kosiodg(at)yahoo(dot)com>
To: "depesz(at)depesz(dot)com" <depesz(at)depesz(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17761: Questionable regular expression behavior
Date: 2023-01-30 08:11:26
Message-ID: 36522775.1746773.1675066286328@mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Yes, greediness seems to be the case,One other solution I found to make it greedy is to add '.*$' at the end:'(a).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?.*$'

В петък, 27 януари 2023 г., 18:05:01 ч. Гринуич+2, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> написа:

hubert depesz lubaczewski <depesz(at)depesz(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Duncan Sands 2023-01-30 09:59:37 range_agg extremely slow compared to naive implementation in obscure circumstances
Previous Message Alvaro Herrera 2023-01-29 19:24:21 Re: BUG #17741: vacuum process hangs after pg_surgery manipulations