1

Why is the following instruction returning FALSE?

SELECT '[1-3]{5}' SIMILAR TO '22222' ;

I can't find what is wrong with that, according to the Postgres doc ...

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
wiltomap
  • 3,933
  • 8
  • 37
  • 54
  • @Erwin : Thank you for correcting my post with the right terms... I'm not a native english speaker so some words might not be appropriate. And I'm still not used to stackoverflow very well. – wiltomap Jul 10 '14 at 07:31

3 Answers3

4

Your basic error has already been answered.
More importantly, don't use SIMILAR TO at all. It's completely pointless:

Use LIKE, or the regular expression match operator ~, or other pattern matching operators:

For 5 digits between 1 and 3 use the expression @Thomas provided.

If you actually want 5 identical digits between 1 and 3 like your example suggests I suggest a back reference:

SELECT '22222' ~ '([1-9])\1{4}';

Related answer with more explanation:

sqlfiddle demonstrating both.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
2

The operator is defined as:

string SIMILAR TO pattern 

so the first parameter is the string that you want to compare. The second parameter is the regex to compare against.

You need:

SELECT '22222' SIMILAR TO '[1-3]{5}';
1

try

SELECT '22222' ~ '[1-3]{5}'

SIMILAR is not POSIX standard

The SIMILAR TO operator returns true or false depending on whether its pattern matches the given string. It is similar to LIKE, except that it interprets the pattern using the SQL standard's definition of a regular expression. SQL regular expressions are a curious cross between LIKE notation and common regular expression notation.

...

POSIX regular expressions provide a more powerful means for pattern matching than the LIKE and SIMILAR TO operators. Many Unix tools such as egrep, sed, or awk use a pattern matching language that is similar to the one described here.

http://www.postgresql.org/docs/9.2/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP

Community
  • 1
  • 1
ThomasEdwin
  • 2,035
  • 1
  • 24
  • 36