Re: Passing function parameters to regexp_replace - Mailing list pgsql-sql

From Tim Landscheidt
Subject Re: Passing function parameters to regexp_replace
Date
Msg-id m3fwjvt7wo.fsf@passepartout.tim-landscheidt.de
Whole thread Raw
In response to Passing function parameters to regexp_replace  (Leif Biberg Kristensen <leif@solumslekt.org>)
Responses Re: Passing function parameters to regexp_replace
List pgsql-sql
Leif Biberg Kristensen <leif@solumslekt.org> wrote:

>> UPDATE sources SET source_text = regexp_replace(source_text,
>> E'n="(.*?)$1(.*?)"', E'n="\\1$2\\2"', 'g') where source_text like
>> '%n="%$2%">%';

> Sorry, I pasted a literal replacement, and substituted the parameters by hand.
> The expression should of course be

> UPDATE sources SET source_text = regexp_replace(source_text,
> E'n="(.*?)$1(.*?)"', E'n="\\1$2\\2"', 'g') where source_text like
> '%n="%$1%">%'

Try:

> UPDATE sources SET source_text = regexp_replace(source_text,
> CONCAT(E'n="(.*?)', $1, E'(.*?)"'), CONCAT(E'n="\\1', $2, '\\2"', 'g') where source_text like
> CONCAT('%n="%', $1, '%">%')

If $1 and $2 (can) include meta characters, you have to es-
cape them properly.
 Please consider that regexp_replace() uses POSIX Regular
Expressions while LIKE uses a different syntax. If possible,
I would replace the LIKE expression with its "~" equivalent
so chances of confusion are minimized.

Tim



pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Use select and update together
Next
From: Leif Biberg Kristensen
Date:
Subject: Re: Passing function parameters to regexp_replace