Re: replace inside regexp_replace

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Oliver Kohll <oliver(at)agilechilli(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: replace inside regexp_replace
Date: 2021-06-21 14:47:52
Message-ID: 20210621144752.GA12013@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-06-21 15:36:48 Re: replace inside regexp_replace
Previous Message Laurenz Albe 2021-06-21 14:27:52 Re: user privileges