Re: Postgres 8.4 literal escaping

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Andreas <maps(dot)on(at)gmx(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Niederland <niederland(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Postgres 8.4 literal escaping
Date: 2009-07-13 15:48:41
Message-ID: 20090713154841.GD4930@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andreas escribió:
> 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?

Just prepend E to the whole string, i.e. instead of
'\\(0\\)||\\(||\\)' use E'\\(0\\)||\\(||\\)'

If you're list most people and hate having to double the \s use
dollar-quoting, as shown below.

BTW it seems the function could be written in the SQL language instead
of plpgsql, which could make it faster.

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

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2009-07-13 15:59:22 Re: PostgreSQL 8.4 packages for Fedora 11?
Previous Message Simon Riggs 2009-07-13 15:20:19 Re: postgres/postgis indexes