pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Daniel Verite <daniel@manitou-mail.org>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@postgresql.org
Subject: Re: Mixing greediness in regexp_matches
Date: Mon, 23 Dec 2019 17:10:41 +0100
Message-ID: <90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org> (raw)
In-Reply-To: <29225.1577114775@sss.pgh.pa.us>

	Tom Lane wrote:

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

It occurred to me too that regexp_split_to_table or array would make
this problem really easy if only it had a mode to capture and return the
matched parts too.

FWIW, in plperl, there's a simple solution:

 $string =~ s/(foobar|foo|...)/$replace{$1}/g

when %replace is a hash of the substitutions %(foo=>baz,...).
The strings in the alternation are tested in their order of
appearance, so you can choose to be greedy or not by just sorting
them by length.


Best regards,
-- 
Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-mail.org
Twitter: @DanielVerite





view thread (6+ messages)  latest in thread

Message-ID: <90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org>
Permalink:  ../90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org/
Also on:    postgresql.org/message-id/90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org

 · 

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: daniel@manitou-mail.org, tgl@sss.pgh.pa.us
  Subject: Re: Mixing greediness in regexp_matches
  In-Reply-To: <90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org>
  References: <29225.1577114775@sss.pgh.pa.us> <90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org>

* 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