REGEXP search condition

Match a pattern against a string.

Syntax
expression [ NOT ] REGEXP pattern [ ESCAPE escape-expression ]
Parameters

expression   The string to be searched.

pattern   The regular expression to search for within expression.

For more information about the syntax for regular expressions, see Regular expressions overview.

escape-expression   The escape character to be used in the match. The default is the backslash character (\).

Remarks

The REGEXP search condition matches a whole string, not a substring. To match on a substring with the string, enclose the string in wildcards that match the rest of the string (.*pattern.*). For example, SELECT ... WHERE Description REGEXP 'car' matches only car, not sportscar. However, SELECT ... WHERE Description REGEXP '.*car' matches car, sportscar, and any string that ends with car. Alternatively, you can rewrite your query to make use the REGEXP_SUBSTR function, which is designed to search for substrings within a string.

When matching against only a sub-character class, you must include the outer square brackets and the square brackets for the sub-character class. For example, expression REGEXP '[[:digit:]]'). For more on sub-character class matching, see Regular expressions: Special sub-character classes.

Database collation and matching

REGEXP only matches a literal in a pattern if it is the exact same character (that is, they have the same code point value). Ranges in character classes (for example, '[A-F]') only match characters that code point values greater than or equal to the code point value of the first character in the range (A) and less than or equal to the code point value of the second character in the range (F).

For a comparison of how matching and range evaluations are handled for LIKE, SIMILAR TO, and REGEXP, see LIKE, REGEXP, and SIMILAR TO search conditions.

Comparisons are performed character-by-character, unlike the equivalence (=) operator and other operators where the comparison is done string-by-string. For example, when a comparison is done in a UCA collation (CHAR or NCHAR with the collation set to UCA), 'Æ'='AE' is true, but 'Æ' REGEXP 'AE' is false.

National character (NCHAR) support

REGEXP search conditions can be used to compare CHAR and NCHAR strings. In this case, character set conversion is performed so that the comparison is done using a common data type. Then, a code point by code point comparison is performed. See Comparisons between CHAR and NCHAR.

You can specify expression or pattern as an NCHAR string literal by prefixing the quoted value with N (for example, expression REGEXP N'pattern'). You can also use the CAST function to cast the pattern to CHAR or NCHAR (for example, expression REGEXP CAST(pattern AS datatype).

See String literals, and CAST function [Data type conversion].

See also