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:26:15 -0500
Message-ID: <29225.1577114775@sss.pgh.pa.us> (raw)
In-Reply-To: <28143.1577114140@sss.pgh.pa.us>
References: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org>
	<28143.1577114140@sss.pgh.pa.us>

"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).

BTW, just to think outside the box a bit, I wonder whether you
couldn't build this out of regexp_split_to_array:

regression=# select regexp_split_to_array('junkfoolbarfoolishfoobarmore', 'foo|bar|foobar');
 regexp_split_to_array 
-----------------------
 {junk,l,"",lish,more}
(1 row)

The idea would be to iterate over the array elements, tracking the
corresponding position in the source string, and re-discovering at
each break which of the original alternatives must've matched.

It's sort of annoying that we don't have a simple "regexp_location"
function that would give you back the starting position of the
first match.

			regards, tom lane





view thread (6+ messages)  latest in thread

Message-ID: <29225.1577114775@sss.pgh.pa.us>
Permalink:  ../29225.1577114775@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/29225.1577114775@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: <29225.1577114775@sss.pgh.pa.us>
  References: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org> <28143.1577114140@sss.pgh.pa.us> <29225.1577114775@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