Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id CE8D0B5DBE1 for ; Sat, 17 Sep 2011 14:28:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 21705-06 for ; Sat, 17 Sep 2011 17:28:00 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail47.e.nsc.no (mail47.e.nsc.no [193.213.115.47]) by mail.postgresql.org (Postfix) with ESMTP id 18F1BB5DBC8 for ; Sat, 17 Sep 2011 14:27:59 -0300 (ADT) Received: from balapapa.localnet (107.100.202.84.customer.cdi.no [84.202.100.107]) (authenticated bits=0) by mail47.nsc.no (8.14.4/8.14.4) with ESMTP id p8HHRuX4011999 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sat, 17 Sep 2011 19:27:57 +0200 (MEST) From: Leif Biberg Kristensen To: pgsql-sql@postgresql.org Subject: Re: Passing function parameters to regexp_replace Date: Sat, 17 Sep 2011 19:27:55 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.39-gentoo-r3; KDE/4.6.5; x86_64; ; ) References: <201109171321.43309.leif@solumslekt.org> <201109171331.58663.leif@solumslekt.org> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201109171927.56033.leif@solumslekt.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/47 X-Sequence-Number: 35847 On Saturday 17. September 2011 19.07.03 Tim Landscheidt wrote: > Leif Biberg Kristensen wrote: > > > 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, '%">%') The function CONCAT doesn't exist i PostgreSQL. And I can't get it to work with EXECUTE and standard concatenation either: pgslekt=> CREATE OR REPLACE FUNCTION update_nametags(TEXT, TEXT) RETURNS VOID AS $$ pgslekt$> BEGIN pgslekt$> EXECUTE $_$ pgslekt$> UPDATE sources SET source_text = pgslekt$> REGEXP_REPLACE( pgslekt$> source_text, pgslekt$> E'n="(.*?)' || $1 || '(.*?)"', pgslekt$> E'n="\\1' || $2 || '\\2"', 'g' pgslekt$> ) pgslekt$> WHERE source_text LIKE E'%n="%' || $1 || '%">%' pgslekt$> $_$; pgslekt$> END pgslekt$> $$ LANGUAGE PLPGSQL VOLATILE; CREATE FUNCTION Time: 1,105 ms pgslekt=> select update_nametags('Brynild','Brynil'); WARNING: nonstandard use of \\ in a string literal LINE 6: E'n="\\1' || $2 || '\\2"', 'g' ^ HINT: Use the escape string syntax for backslashes, e.g., E'\\'. QUERY: UPDATE sources SET source_text = REGEXP_REPLACE( source_text, E'n="(.*?)' || $1 || '(.*?)"', E'n="\\1' || $2 || '\\2"', 'g' ) WHERE source_text LIKE E'%n="%' || $1 || '%">%' CONTEXT: PL/pgSQL function "update_nametags" line 2 at EXECUTE statement ERROR: there is no parameter $1 LINE 5: E'n="(.*?)' || $1 || '(.*?)"', ^ QUERY: UPDATE sources SET source_text = REGEXP_REPLACE( source_text, E'n="(.*?)' || $1 || '(.*?)"', E'n="\\1' || $2 || '\\2"', 'g' ) WHERE source_text LIKE E'%n="%' || $1 || '%">%' CONTEXT: PL/pgSQL function "update_nametags" line 2 at EXECUTE statement > 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. The intended use is to replace a short string like 'Jacob' with 'Jakob' within a specific XML attribute value. regards, Leif