Pattern Matching Operators

There are three separate approaches to pattern matching provided by the database: the traditional SQL LIKE operator, the more recent SIMILAR TO 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).

SIMILAR TO

The SIMILAR TO operator determines whether to match a given string based on its own pattern and returns true or false. It is similar to LIKE, except that it interprets the pattern using the SQL standard's definition of a regular expression.

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 2 lists all available operators for POSIX regular expression pattern matching.

Table 2 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.*'