Re: Postgres 8.4 literal escaping

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Niederland <niederland(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Postgres 8.4 literal escaping
Date: 2009-07-13 14:53:58
Message-ID: 4A5B4A86.3050306@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I've got a similar issue with a function that uses regular-expression-magic.
I got it from the sql list and it works but I'm just about 75% aware of how.
Still PG complains about those \\ everywhere.

Replacing every \ by || E'\\' || would make it ... cough ... not
looking cuter as it allready is.
What would be the correct syntax here?

I need it to search phone numbers.
The function strips all characters that are no number or "+" out of the
input-string.
If the international part is "+49" or "0049" it get reduced to 0.

CREATE OR REPLACE FUNCTION cleanphonenr(text)
RETURNS text AS
$BODY$
BEGIN
RETURN CASE
WHEN regexp_replace($1, E'[^0-9+]', '', 'g') ~ E'^(\\+|00)49'
THEN '0'||
regexp_replace(
regexp_replace(
regexp_replace($1, E'[^0-9+()]', '', 'g')
, '\\(0\\)||\\(||\\)', '', 'g')
, E'^(?:\\+|00)49(.*)', E'\\1')
WHEN regexp_replace($1, E'[^0-9+]', '', 'g') ~ E'^(\\+|00)'
THEN '+'||
regexp_replace(
regexp_replace(
regexp_replace($1, E'[^0-9+()]', '', 'g')
, '\\(0\\)||\\(||\\)', '', 'g')
, E'^(?:\\+||00)(.*)', E'\\1')
ELSE
regexp_replace($1, E'[^0-9]', '', 'g')
END;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2009-07-13 14:54:54 Re: uuid_hash declaration
Previous Message Raymond O'Donnell 2009-07-13 14:53:35 Re: Question]