Re: 9.5.3: substring: regex greedy operator not picking up chars as expected

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "Foster, Russell" <Russell(dot)Foster(at)crl(dot)com>
Cc: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: 9.5.3: substring: regex greedy operator not picking up chars as expected
Date: 2016-08-15 12:44:57
Message-ID: CAKFQuwaAt6wYJQjKM9i-jm7hmfbi0ptiEt4SN8_vGQ43V+z-5Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

​Working as documented.​

https://www.postgresql.org/docs/9.5/static/functions-matching.html#POSIX-MATCHING-RULES

Specifically, this implementation considers greediness at a level higher
than just the atom/expression - and in a mixed "branch" if there is a
non-greedy quantifier in a branch the entire branch is non-greedy and can
in many situations cause greedy atoms to behave non-greedily.

In might help to consider that there aren't really any explicit "greedy"
operators like other engines have (i.e., ??, ?, ?+) but rather non-greedy
(lazy) and default. The default inherits the non-greedy trait from its
parent if applicable otherwise is behaves greedily.

On Mon, Aug 15, 2016 at 7:53 AM, Foster, Russell <Russell(dot)Foster(at)crl(dot)com>
wrote:

> Hello,
>
>
>
> For the following query:
>
>
>
> select substring('>772' from '.*?[0-9]+')
>

​The pattern itself is non-greedy​ due to their only being a single branch
and it having a non-greedy quantifier within it.

.*? matches ">" and [0-9]+ only needs a single character to generate a
non-greedy match conforming match

>
> I would expect the output to be ‘>772’, but it is ‘>7’. You can also see
> the expected result on https://regex101.com/, although I am aware not all
> regex processors work the same.
>
>
>
> The following queries:
>
>
>
> select substring('>772' from '^.*?[0-9]+$')
>

​This is treated exactly the same as the above but because of the ^$ the
shortest possible output string is the entire string​

>
> and:
>
>
>
> select substring('>772' from '[0-9]+')
>
>
>
> both return ‘>772’, which is expected. Could the less greedy operator on
> the left (.*?) be affecting the more greedy right one (+)?
>
>
>

Typo here? I'm not fluent with substring(regex).

Anyway, the entire RE (single branch) is now greedy so the greedy [0-9]+
atom matches as many numbers as possible.

David J.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Foster, Russell 2016-08-15 12:55:16 Re: 9.5.3: substring: regex greedy operator not picking up chars as expected
Previous Message boris.djurdjevic 2016-08-15 12:20:56 Re: BUG #14288: sd_notify not called on startup using rhel RPMs