Regex Character-Class

Started by Henryalmost 17 years ago4 messagesgeneral
Jump to latest
#1Henry
henry@zen.co.za

Greets,

I must be missing something here:

SELECT '1.1.1.1' ~ E'^\d+';

returns FALSE, when I would expect TRUE, as for:

SELECT '1.1.1.1' ~ E'^[[:digit:]]+';

ie, '[[:digit:]]' != '\d'

In config, "regex_flavor = advanced".

Any ideas?

Thanks
Henry

#2A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Henry (#1)
Re: Regex Character-Class

In response to Henry :

Greets,

I must be missing something here:

SELECT '1.1.1.1' ~ E'^\d+';

returns FALSE, when I would expect TRUE, as for:

Try:

test=*# SELECT '1.1.1.1' ~ E'^\\d+';
?column?
----------
t
(1 row)

Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

#3Thomas Pundt
mlists@rp-online.de
In reply to: Henry (#1)
Re: Regex Character-Class

Henry schrieb:

I must be missing something here:

SELECT '1.1.1.1' ~ E'^\d+';

returns FALSE, when I would expect TRUE, as for:

SELECT '1.1.1.1' ~ E'^[[:digit:]]+';

ie, '[[:digit:]]' != '\d'

In config, "regex_flavor = advanced".

Any ideas?

Yes; you have to escape the backslash character:

=> SELECT '1.1.1.1' ~ E'^\\d+';
?column?
----------
t
(1 row)

See the documentation for this
(http://www.postgresql.org/docs/8.3/interactive/functions-matching.html):

Note: Remember that the backslash (\) already has a special meaning
in PostgreSQL string literals. To write a pattern constant that
contains a backslash, you must write two backslashes in the statement,
assuming escape string syntax is used (see Section 4.1.2.1).

Ciao,
Thomas

#4Henry
henry@zen.co.za
In reply to: A. Kretschmer (#2)
Re: Regex Character-Class

Quoting "A. Kretschmer" <andreas.kretschmer@schollglas.com>:

Try:

test=*# SELECT '1.1.1.1' ~ E'^\\d+';

Ag, of course, thanks Andreas.

Cheers
Henry