For the complete documentation index, see llms.txt. This page is also available as Markdown.

REGEXP

Complete REGEXP operator reference: expr REGEXP/RLIKE pat syntax, 1/0/NULL return values, NOT REGEXP aliases, BINARY case-sensitivity, default_regex_flags.

Syntax

expr REGEXP pat, expr RLIKE pat

Description

Performs a pattern match of a string expression expr against a patternpat. The pattern can be an extended regular expression. See Regular Expressions Overview for details on the syntax for regular expressions (see also PCRE Regular Expressions).

Returns 1 if expr matches pat or 0 if it doesn't match. If either expr or pat are NULL, the result is NULL.

The pattern matches if it occurs anywhere in expr. To match the whole string, anchor the pattern with ^ and $.

The negative form NOT REGEXP also exists, as an alias for NOT (string REGEXP pattern). RLIKE and NOT RLIKE are synonyms for REGEXP and NOT REGEXP, originally provided for mSQL compatibility.

The pattern need not be a literal string. For example, it can be specified as a string expression or table column.

Note: Because MariaDB uses the C escape syntax in strings (for example, \n to represent the newline character), you must double any backslash (\) that you use in your REGEXP strings.

REGEXP is not case sensitive, except when used with a binary string, a string with a binary collation such as utf8mb4_bin, or a string with certain case-sensitive collations such as latin1_general_cs. Some case-sensitive collations, including the UCA-based _as_cs collations such as utf8mb4_uca1400_as_cs, still match case-insensitively. To force a case-sensitive match, use BINARY or start the pattern with (?-i).

The default_regex_flags variable addresses the remaining compatibilities between PCRE and the old regex library.

Examples

SELECT 'Monty!' REGEXP 'm%y%%';
+-------------------------+
| 'Monty!' REGEXP 'm%y%%' |
+-------------------------+
|                       0 |
+-------------------------+

SELECT 'Monty!' REGEXP '.*';
+----------------------+
| 'Monty!' REGEXP '.*' |
+----------------------+
|                    1 |
+----------------------+

SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';
+---------------------------------------+
| 'new*\n*line' REGEXP 'new\\*.\\*line' |
+---------------------------------------+
|                                     0 |
+---------------------------------------+

SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
+----------------+-----------------------+
| 'a' REGEXP 'A' | 'a' REGEXP BINARY 'A' |
+----------------+-----------------------+
|              1 |                     0 |
+----------------+-----------------------+

SELECT 'a' REGEXP '^[a-d]';
+---------------------+
| 'a' REGEXP '^[a-d]' |
+---------------------+
|                   1 |
+---------------------+

In the 'new*\n*line' example, doubling the backslashes makes the pattern match literal asterisks, but the statement still returns 0, because . does not match a newline character unless the DOTALL flag is set with default_regex_flags.

default_regex_flags examples

MariaDB uses the default_regex_flags variable to address the remaining compatibilities between PCRE and the old regex library.

The default behavior (multiline match is off)

Enabling the multiline option using the PCRE option syntax:

Enabling the multiline option using default_regex_flags

An unanchored pattern matches anywhere in the string:

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sql

spinner

Last updated

Was this helpful?