regexp_replace and search/replace values stored in table - Mailing list pgsql-sql

From Leif Biberg Kristensen
Subject regexp_replace and search/replace values stored in table
Date
Msg-id 201004271343.48070.leif@solumslekt.org
Whole thread Raw
Responses Re: regexp_replace and search/replace values stored in table  (Leif Biberg Kristensen <leif@solumslekt.org>)
Re: regexp_replace and search/replace values stored in table  (Leif Biberg Kristensen <leif@solumslekt.org>)
List pgsql-sql
I've got a system for entering and storing a lot of standard hyperlinks in a 
compact form, and then expand them at run time like this:

CREATE OR REPLACE FUNCTION _my_expand(TEXT) RETURNS TEXT AS $$
-- private func, expand various compact links
DECLARE   str TEXT = $1;
BEGIN   -- Scanned church books [kb=book reference|image reference|link text]   str := REGEXP_REPLACE(str,
E'\\[kb=(.+?)\\|(.+?)\\|(.+?)\\]',          E'<a 
 
href="//www.arkivverket.no/URN:kb_read?idx_kildeid=\\1&uid=ny&idx_side=\\2" 
title="Lenke til bilde av kirkebokside">\\3</a>', 'g');   -- Scanned probate registers [sk=protocol|image
reference|linktext]   str := REGEXP_REPLACE(str, E'\\[sk=(.+?)\\|(.+?)\\|(.+?)\\]',           E'<a
href="//www.arkivverket.no/URN:sk_read/\\1/\\2"title="Lenke 
 
til bilde av skifteprotokollside">\\3</a>', 'g');   -- Scanned deed ("pantebok") registers [sk=protocol|image
reference|link
 
text]   str := REGEXP_REPLACE(str, E'\\[tl=(.+?)\\|(.+?)\\|(.+?)\\]',           E'<a 
href="//www.arkivverket.no/URN:tl_read?idx_id=\\1&uid=ny&idx_side=\\2" 
title="Lenke til bilde av pantebokside">\\3</a>', 'g');   RETURN str;
END
$$ LANGUAGE plpgsql STABLE;

According to the slogan "minimize code, maximize data" I feel that these 
strings should be stored in a table:

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'<a href="//www.arkivverket.no/URN:sk_read/\\\\1/\\\\2" title="Lenke til 
bilde av skifteprotokollside">\\\\3</a>',
'Scanned probate registers [sk=protocol|image reference|link text]');

pgslekt=> select * from short_links;
link_type |           short_link           |                                                 
long_link                                                 |                            
description

-----------+--------------------------------+-----------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------sk
      | \\[sk=(.+?)\\|(.+?)\\|(.+?)\\] | <a 
 
href="//www.arkivverket.no/URN:sk_read/\\1/\\2" title="Lenke til bilde av 
skifteprotokollside">\\3</a> | Scanned probate registers [sk=protocol|image 
reference|link text]
(1 row)

So far, so good. But when I try to do the actual expansion, I'm stumped.

pgslekt=> select regexp_replace((select source_text from sources where 
source_id=23091), (select quote_literal(short_link) from short_links where 
link_type = 'sk'), (select quote_literal(long_link) from short_links where 
link_type = 'sk'), 'g');                                        regexp_replace
------------------------------------------------------------------------------------------------[sk=25658|67|side 66a].
Vabakkenunder Klyve vestre i Solum 07.07.1784: 
 
[p=6947|Isach Jonsen].
(1 row)

What am I missing?

regards,
-- 
Leif Biberg Kristensen
http://solumslekt.org/


pgsql-sql by date:

Previous
From: junaidmalik14
Date:
Subject: Re: count function alternative in postgres
Next
From: Tim Landscheidt
Date:
Subject: Re: regexp_replace and search/replace values stored in table