Updated on 2026-11-06 GMT+08:00

Pattern Matching Operators

There are three separate approaches to pattern matching provided by the database: SQL LIKE operator and POSIX-style regular expressions. Besides these basic operators, functions can be used to extract or replace matching substrings and to split a string at matching locations.

LIKE

Checks whether the string matches the pattern string following LIKE. If the string matches the supplied pattern, the LIKE expression returns true (the NOT LIKE expression returns false). Otherwise, the LIKE expression returns false (the NOT LIKE expression returns true).

  • Rule
    1. This operator can succeed only when its pattern matches the entire string. If you want to match a sequence in any position within the string, the pattern must begin and end with a percent sign.
    2. An underscore (_) represents (matches) any single character, and a percent sign (%) represents a wildcard for any string.
    3. To match a literal underscore (_) or percent sign (%), the corresponding character in pattern must be preceded by the escape character. The role of an escape character is to disable the special meaning of metacharacters. By default, the escape character is a backslash, but a different escape character can also be specified using the ESCAPE clause.
    4. To match the escape character itself, use two escape characters. For example: To write a pattern constant containing a backslash (\), you need to enter two backslashes in SQL statements.

      When standard_conforming_strings is set to off, any backslashes you write in literal string constants will need to be doubled. Therefore, writing a pattern matching a single backslash is actually going to write four backslashes in the statement. You can avoid this by selecting a different escape character by using ESCAPE, so that the backslash is no longer a special character of LIKE. But the backslash is still the special character of the character text analyzer, so you still need two backslashes. You can also select no escape character by writing ESCAPE ''. This effectively disables the escape mechanism, which makes it impossible to turn off the special meaning of underscore and percent signs in the pattern.

    5. The keyword ILIKE can be used instead of LIKE to make the match case-insensitive.
    6. Operator ~~ is equivalent to LIKE, and operator ~~* corresponds to ILIKE.
  • Examples
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' LIKE 'abc' AS RESULT;
     result
    -----------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' LIKE 'a%' AS RESULT;
     result
    -----------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' LIKE '_b_' AS RESULT;
     result
    -----------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' LIKE 'c' AS RESULT;
     result
    -----------
     f
    (1 row)
    

POSIX regular expressions

A regular expression is a character sequence that is an abbreviated definition of a set of strings (a regular set). If a string is a member of a regular expression described by a regular expression, the string matches the regular expression. POSIX regular expressions provide a more powerful means for pattern matching than the LIKE and SIMILAR TO operators. Table 1 lists all available operators for POSIX regular expression pattern matching.

Table 1 Regular expression match operators

Operator

Description

Example

~

Matches regular expression, which is case-sensitive.

'thomas' ~ '.*thomas.*'

~*

Matches regular expression, which is case-insensitive.

'thomas' ~* '.*Thomas.*'

!~

Does not match regular expression, which is case-sensitive.

'thomas' !~ '.*Thomas.*'

!~*

Does not match regular expression, which is case-insensitive.

'thomas' !~* '.*vadim.*'

  • Matching rule
    1. Unlike LIKE patterns, a regular expression is allowed to match anywhere within a string, unless the regular expression is explicitly anchored to the beginning or end of the string.
    2. Besides the metacharacters mentioned above, POSIX regular expressions also support the following pattern matching metacharacters:
      Table 2 Pattern matching metacharacters

      Metacharacter

      Description

      ^

      Specifies the match starting with a string.

      $

      Specifies the match at the end of a string.

      .

      Matches any single character.

  • Regular expression functions
    POSIX regular expressions support the following functions:
  • Examples
    1
    2
    3
    4
    5
    postgres=#  SELECT 'abc' ~ 'Abc' AS RESULT;
    result 
    --------
     f
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' ~* 'Abc' AS RESULT;
     result 
    --------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' !~ 'Abc' AS RESULT;
     result 
    --------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc'!~* 'Abc' AS RESULT;
     result 
    --------
     f
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' ~ '^a' AS RESULT;
     result 
    --------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' ~ '(b|d)'AS RESULT;
     result 
    --------
     t
    (1 row)
    
    1
    2
    3
    4
    5
    postgres=# SELECT 'abc' ~ '^(b|c)'AS RESULT;
     result 
    --------
     f
    (1 row)
    

    Although most regular expression searches can be executed quickly, the time and memory for regular expression processing can still be manually controlled. It is not recommended that you accept the regular expression search mode from the non-security mode source. If you must do this, you are advised to add the statement timeout limit. The search with the SIMILAR TO mode has the same security risks as the SIMILAR TO provides many capabilities that are the same as those of the POSIX- style regular expression. The LIKE search is much simpler than the other two options. Therefore, it is more secure to accept the non-secure mode source search.