Re: Emulating flexible regex replace - Mailing list pgsql-general

From Tom Lane
Subject Re: Emulating flexible regex replace
Date
Msg-id 25041.1414073733@sss.pgh.pa.us
Whole thread Raw
In response to Emulating flexible regex replace  (twoflower <standa.kurik@gmail.com>)
List pgsql-general
twoflower <standa.kurik@gmail.com> writes:
> Supposing *source* matches the /source pattern/, the $/n/ expressions inside
> the /target pattern/ correspond to the captured groups inside *source*.

> Example:

> Source: 123 source text
> Target: 123 target text
> Source pattern: ([0-9]+) source text
> Target pattern: $1 target text

> This yields a successful match since $1 in the /target pattern/ is replaced
> by "123" from the first captured group in *source* and the resulting string,
> "123 target text", matches the /target pattern/.

> I would like to execute a query which for a given /source pattern/ and
> /target pattern/ returns all rows from the *SEGMENT* table where *source*
> matches the /source pattern/ and *target* matches the /target pattern/ after
> it has its references replaced with the actual captured groups.

> I believe this is not possible since *regexp_replace* expects a string as
> its /replacement/ argument which is not enough in this case.

Well, you could pull out the source text captures with regexp_matches,
escape them somehow (don't think there's a built-in function for that),
insert them into the target pattern with regexp_replace, and then apply
the target pattern with a simple regexp match operator.  Kinda tedious,
but hardly "not possible".

A lot of people feel that this sort of text-mashing requirement is best
handled in plperl, but if you don't want to use that for some reason,
it's surely possible in plpgsql.

            regards, tom lane


pgsql-general by date:

Previous
From: twoflower
Date:
Subject: Emulating flexible regex replace
Next
From: David G Johnston
Date:
Subject: Re: Emulating flexible regex replace