From: hubert depesz lubaczewski <depesz@depesz.com>
To: Oliver Kohll <oliver@agilechilli.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: replace inside regexp_replace
Date: Mon, 21 Jun 2021 16:47:52 +0200
Message-ID: <20210621144752.GA12013@depesz.com> (raw)
In-Reply-To: <CAMS=m5+sh7=4jtEGmgxyQ8OfiL=5YM8DUYQN5UxKZBdtotBsNQ@mail.gmail.com>
References: <CAMS=m5+sh7=4jtEGmgxyQ8OfiL=5YM8DUYQN5UxKZBdtotBsNQ@mail.gmail.com>
On Mon, Jun 21, 2021 at 02:27:22PM +0100, Oliver Kohll wrote:
> It half works, i.e. it removes the brackets but doesn't seem to process the
> inner replace. It's as if the select were just
> select regexp_replace(
> 'here is [[my text]] to replace and [[some more]]',
> E'\\[\\[(.*?)\\]\\]',
> E'\\1',
> 'g'
> );
> I've a feeling I'm missing something fundamental, any idea what?
\1 works only if it's argument to regexp_replace.
And regexp_replace can't call any other functions.
What you could do is:
$ select string_agg(x[1] || replace(x[2], ' ', '_') || x[3], '') from regexp_matches( 'here is [[my text]] to replace and [[some more]] and maybe [[a bit longer]] too', '(.*?)\[\[(.*?)\]\](.*?)', 'g') x;
string_agg
─────────────────────────────────────────────────────────────────
here is my_text to replace and some_more and maybe a_bit_longer
(1 row)
Or just use plperl, pl/python, or anything like this.
Best regards,
depesz
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: depesz@depesz.com, oliver@agilechilli.com
Subject: Re: replace inside regexp_replace
In-Reply-To: <20210621144752.GA12013@depesz.com>
References: <CAMS=m5+sh7=4jtEGmgxyQ8OfiL=5YM8DUYQN5UxKZBdtotBsNQ@mail.gmail.com> <20210621144752.GA12013@depesz.com>
* 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