Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 456F6B5DC35 for ; Sat, 17 Sep 2011 14:07:26 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 97376-06 for ; Sat, 17 Sep 2011 17:07:19 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 87D00B5DC33 for ; Sat, 17 Sep 2011 14:07:19 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1R4yMF-0002aQ-25 for pgsql-sql@postgresql.org; Sat, 17 Sep 2011 19:07:15 +0200 Received: from f054053076.adsl.alicedsl.de ([78.54.53.76]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 17 Sep 2011 19:07:15 +0200 Received: from tim by f054053076.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 17 Sep 2011 19:07:15 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: Passing function parameters to regexp_replace Date: Sat, 17 Sep 2011 17:07:03 +0000 Organization: Lines: 28 Message-ID: References: <201109171321.43309.leif@solumslekt.org> <201109171331.58663.leif@solumslekt.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: f054053076.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:JKMXhP/XvJKdYMOZfo8A2HxGOF8= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/46 X-Sequence-Number: 35846 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="%$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