Re: replace inside regexp_replace - Mailing list pgsql-general

From hubert depesz lubaczewski
Subject Re: replace inside regexp_replace
Date
Msg-id 20210621144752.GA12013@depesz.com
Whole thread Raw
In response to replace inside regexp_replace  (Oliver Kohll <oliver@agilechilli.com>)
List 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




pgsql-general by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: user privileges
Next
From: "David G. Johnston"
Date:
Subject: Re: replace inside regexp_replace