Using SIMILAR TO for a regex?

后端 未结 3 1935
一整个雨季
一整个雨季 2021-01-28 18:01

Why is the following instruction returning FALSE?

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

I can\'t find what is wrong with that

3条回答
  •  日久生厌
    2021-01-28 18:58

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

    • Query performance in PostgreSQL using 'similar to'
    • Difference between LIKE and ~ in Postgres

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

    • Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL

    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:
    Deleting records with number repeating more than 5

    SQL Fiddle demonstrating both.

提交回复
热议问题