Received: from maia.hub.org (maia-4.hub.org [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 6C000632563 for ; Tue, 27 Apr 2010 10:32:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 50663-05 for ; Tue, 27 Apr 2010 13:32:29 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail48.e.nsc.no (mail48.e.nsc.no [193.213.115.48]) by mail.postgresql.org (Postfix) with ESMTP id 64276632BEC for ; Tue, 27 Apr 2010 10:32:26 -0300 (ADT) Received: from balapapa.localnet (42.185.16.62.customer.cdi.no [62.16.185.42]) (authenticated bits=0) by mail48.nsc.no (8.14.3/8.14.3) with ESMTP id o3RDWK5R021428 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Tue, 27 Apr 2010 15:32:24 +0200 (MEST) From: Leif Biberg Kristensen To: pgsql-sql@postgresql.org Subject: Re: regexp_replace and search/replace values stored in table Date: Tue, 27 Apr 2010 15:32:20 +0200 User-Agent: KMail/1.12.4 (Linux/2.6.32-gentoo-r7; KDE/4.3.5; i686; ; ) References: <201004271343.48070.leif@solumslekt.org> In-Reply-To: <201004271343.48070.leif@solumslekt.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201004271532.20639.leif@solumslekt.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.8 tagged_above=-10 required=5 tests=BAYES_50=0.8 X-Spam-Level: X-Archive-Number: 201004/99 X-Sequence-Number: 34026 On Tuesday 27. April 2010 13.43.48 Leif Biberg Kristensen wrote: > CREATE TABLE short_links ( > link_type CHAR(2) PRIMARY KEY, > short_link TEXT, > long_link TEXT, > description TEXT > ); > > It appears like I have to double the number of backslashes when I enter the > data: > > INSERT INTO short_links (link_type, short_link, long_link, description) VALUES > ('sk', E'\\\\[sk=(.+?)\\\\|(.+?)\\\\|(.+?)\\\\]', > E'\\\\3', > 'Scanned probate registers [sk=protocol|image reference|link text]'); I found the solution. I was totally wrong about the 4 backslashes. pgslekt=> delete from short_links; DELETE 1 pgslekt=> INSERT INTO short_links (link_type, short_link, long_link, description) VALUES pgslekt-> ('sk', E'\\[sk=(.+?)\\|(.+?)\\|(.+?)\\]', pgslekt(> E'\\3', pgslekt(> 'Scanned probate registers [sk=protocol|image reference|link text]'); INSERT 0 1 pgslekt=> select regexp_replace((select source_text from sources where source_id=23091), (select short_link from long_link from short_links where link_type = 'sk'), 'g'); regexp_replace ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- side 66a. Vabakken under Klyve vestre i Solum 07.07.1784: [p=6947|Isach Jonsen]. (1 row) Beautiful. Now I'm going replace this big ugly function with a loop reading values from a table. I may even write an interface for managing shortlinks :) regards, -- Leif Biberg Kristensen http://solumslekt.org/