Re: Using functions in regexp replace captures

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Tim Uckun <timuckun(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using functions in regexp replace captures
Date: 2021-08-05 05:19:28
Message-ID: CAKFQuwYsX_=oNa6RSLhtkE10fk-Z1v8MwAFzJ9ChvKQzhJNeHQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wednesday, August 4, 2021, Tim Uckun <timuckun(at)gmail(dot)com> wrote:

> I want to do something like this
>
> SELECT REGEXP_REPLACE('some_string','(.*)
> (.*)',some_function_that_returns_string('\2',' \1'));
>
> Is this possible at all?
>
>
Generally I’d say yes, it is possible to combine multiple subqueries
together to get the desired end result. Using regexp_match and performing
the conversion on its result is fairly trivial. In theory then write
regexp_replace like above but ignore the capture groups and just stick in
the column into,which you saved the computed value as the direct and
complete replacement.

But no, you cannot directly write: f(x, y, g(a)) where a is the
replacement string because you don’t know what a is when the inner function
g is evaluated first. You need: f(x, y, g(h(x, y))) where h is the
matching function, g is the transform, f is the replacement of the third
argument into the x source text, and y is the pattern. I presume the y is
going to be the same value here but that isn’t required.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-08-05 05:22:54 Re: Using functions in regexp replace captures
Previous Message Tim Uckun 2021-08-05 04:53:38 Using functions in regexp replace captures