Re: Non-capturing expressions

From: Thom Brown <thom(at)linux(dot)com>
To: Francisco Olarte <folarte(at)peoplecall(dot)com>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Non-capturing expressions
Date: 2014-10-25 09:55:43
Message-ID: CAA-aLv60OPWQzQY3bF3bDbFYuLCTzSwaWObxuKNJxqq9+19X9Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 25 October 2014 11:49, Francisco Olarte <folarte(at)peoplecall(dot)com> wrote:

> Hi Thom:
>
> On Sat, Oct 25, 2014 at 11:24 AM, Thom Brown <thom(at)linux(dot)com> wrote:
>
>> It must be that I haven't had enough caffeine today, but I can't figure
>> out why the following expression captures the non-capturing part of the
>> text:
>> # SELECT regexp_matches('postgres','(?:g)r');
>> regexp_matches
>> ----------------
>> {gr}
>> (1 row)
>>
>
> Section 9.7.3, search for 'If the pattern contains no parenthesized
> subexpressions, then each row returned is a single-element text array
> containing the substring matching the whole pattern.'
>

Ah, I knew I missed something:

# SELECT regexp_matches('postgres','(?:g)(r)');
regexp_matches
----------------
{r}
(1 row)

Although I can see it's redundant in this form.

>
>> I'm expecting '{r}' in the output as I thought this would use ARE mode by
>> default.
>>
>
> Why r ? Your pattern is exactly the same as 'gr'. NOTHING gets captured.
> To get that you'll need the opposite 'g(r)' to capture it. By default
> nothing gets captured, the (?:...) construction is used because (....) does
> GROUPING and CAPTURING, and sometimes you want grouping WITHOUT capturing.
>

I'm familiar with regular expression syntax, just famliarising myself with
PostgreSQL's syntax flavour.

Thanks

Thom

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Kohll - Mailing Lists 2014-10-25 10:50:46 Re: dblink password required
Previous Message Francisco Olarte 2014-10-25 09:31:48 Re: Emulating flexible regex replace