pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Daniel Verite <daniel@manitou-mail.org>
Cc: pgsql-general@postgresql.org
Subject: Re: Mixing greediness in regexp_matches
Date: Mon, 23 Dec 2019 10:15:40 -0500
Message-ID: <28143.1577114140@sss.pgh.pa.us> (raw)
In-Reply-To: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org>
References: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org>

"Daniel Verite" <daniel@manitou-mail.org> writes:
> The basic idea is to iterate on the rows produced by
>    regexp_matches(string, '(.*?)(foo|bar|foobar)', 'g')
> to break down the string into pairs of (non-matching segment,
> matching segment) so that a final result can be assembled
> from that (setting aside the last non-matching segment, that
> can be retrieved in a final step).
> The difficulty is that the longest strings in the alternation
> should be prioritized, but the starting (.*?) makes the RE
> non-greedy so "foo" is choosen over "foobar". 

I'd try forcing the match to be the whole string, ie

	^(.*?)(foo|bar|foobar)(.*)$

which would also save some work for restarting the iteration,
since you'd have already captured the all-the-rest substring.

With the endpoints forced, it doesn't really matter whether
the engine deems the RE-as-a-whole to be greedy or not.
I think this would work without needing any explicit greediness
marking for the second and third parts, but I might be wrong
about that detail.

			regards, tom lane





view thread (6+ messages)  latest in thread

Message-ID: <28143.1577114140@sss.pgh.pa.us>
Permalink:  ../28143.1577114140@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/28143.1577114140@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 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-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, daniel@manitou-mail.org
  Subject: Re: Mixing greediness in regexp_matches
  In-Reply-To: <28143.1577114140@sss.pgh.pa.us>
  References: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org> <28143.1577114140@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