Re: Regular Expression Matching problem...

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Regular Expression Matching problem...
Date: 2006-01-04 17:00:31
Message-ID: 20060104170031.GB18628@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Jan 04, 2006 at 03:50:31PM +0100, A. Kretschmer wrote:
> Try this:
>
> test=# select '+385911234567', operator_phonenumber_pattern, '+385911234567' ~ replace(operator_phonenumber_pattern,'\\\\','\\') from operators;
> ?column? | operator_phonenumber_pattern | ?column?
> ---------------+------------------------------+----------
> +385911234567 | ^\\+38590\\d{6,7}$ | f
> +385911234567 | ^\\+38591\\d{7}$ | t
> +385911234567 | ^\\+3859[9|8]\\d{6,7}$ | f
> +385911234567 | ^\\+38595\\d{7}$ | f
> (4 rows)

What Andreas is saying is that the patterns in the table have too
many backslashes. The original query was

test=> select '+385911234567' ~ '^\\+38591\\d{7}$';
?column?
----------
t
(1 row)

but if you select just the pattern you get

test=> select '^\\+38591\\d{7}$';
?column?
----------------
^\+38591\d{7}$
(1 row)

which isn't what's in the table; somehow the patterns in the table
are over-escaped. You might want to read up on how backslashes are
handled in quoted strings, especially when those strings are used
as regular expressions. If you're using 8.0 or later then also
read about dollar-quoted strings.

http://www.postgresql.org/docs/8.1/interactive/sql-syntax.html#SQL-SYNTAX-STRINGS
http://www.postgresql.org/docs/8.1/interactive/functions-matching.html#FUNCTIONS-POSIX-REGEXP

(Use the documentation for whatever version of PostgreSQL you're
running; the above links have some comments that apply only to 8.1
and future versions).

--
Michael Fuhr

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Scott, Casey 2006-01-04 19:54:50 JOIN question with multiple records
Previous Message A. Kretschmer 2006-01-04 14:50:31 Re: Regular Expression Matching problem...