MySQL :: MySQL 8.0 Reference Manual :: 14.8.2 Regular Expressions (original) (raw)

Regular Expression Function and Operator Descriptions

mysql> SELECT 'Michael!' REGEXP '.*';  
+------------------------+  
| 'Michael!' REGEXP '.*' |  
+------------------------+  
|                      1 |  
+------------------------+  
mysql> SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';  
+---------------------------------------+  
| 'new*\n*line' REGEXP 'new\\*.\\*line' |  
+---------------------------------------+  
|                                     0 |  
+---------------------------------------+  
mysql> SELECT 'a' REGEXP '^[a-d]';  
+---------------------+  
| 'a' REGEXP '^[a-d]' |  
+---------------------+  
|                   1 |  
+---------------------+  
mysql> SELECT REGEXP_INSTR('dog cat dog', 'dog');  
+------------------------------------+  
| REGEXP_INSTR('dog cat dog', 'dog') |  
+------------------------------------+  
|                                  1 |  
+------------------------------------+  
mysql> SELECT REGEXP_INSTR('dog cat dog', 'dog', 2);  
+---------------------------------------+  
| REGEXP_INSTR('dog cat dog', 'dog', 2) |  
+---------------------------------------+  
|                                     9 |  
+---------------------------------------+  
mysql> SELECT REGEXP_INSTR('aa aaa aaaa', 'a{2}');  
+-------------------------------------+  
| REGEXP_INSTR('aa aaa aaaa', 'a{2}') |  
+-------------------------------------+  
|                                   1 |  
+-------------------------------------+  
mysql> SELECT REGEXP_INSTR('aa aaa aaaa', 'a{4}');  
+-------------------------------------+  
| REGEXP_INSTR('aa aaa aaaa', 'a{4}') |  
+-------------------------------------+  
|                                   8 |  
+-------------------------------------+  
mysql> SELECT REGEXP_LIKE('CamelCase', 'CAMELCASE');  
+---------------------------------------+  
| REGEXP_LIKE('CamelCase', 'CAMELCASE') |  
+---------------------------------------+  
|                                     1 |  
+---------------------------------------+  
mysql> SELECT REGEXP_LIKE('CamelCase', 'CAMELCASE' COLLATE utf8mb4_0900_as_cs);  
+------------------------------------------------------------------+  
| REGEXP_LIKE('CamelCase', 'CAMELCASE' COLLATE utf8mb4_0900_as_cs) |  
+------------------------------------------------------------------+  
|                                                                0 |  
+------------------------------------------------------------------+  

matchtype may be specified with the c or i characters to override the default case sensitivity. Exception: If either argument is a binary string, the arguments are handled in case-sensitive fashion as binary strings, even if matchtype contains the i character.
Note
MySQL uses C escape syntax in strings (for example,\n to represent the newline character). If you want your_expr_ or_pat_ argument to contain a literal \, you must double it. (Unless theNO_BACKSLASH_ESCAPES SQL mode is enabled, in which case no escape character is used.)

mysql> SELECT REGEXP_LIKE('Michael!', '.*');  
+-------------------------------+  
| REGEXP_LIKE('Michael!', '.*') |  
+-------------------------------+  
|                             1 |  
+-------------------------------+  
mysql> SELECT REGEXP_LIKE('new*\n*line', 'new\\*.\\*line');  
+----------------------------------------------+  
| REGEXP_LIKE('new*\n*line', 'new\\*.\\*line') |  
+----------------------------------------------+  
|                                            0 |  
+----------------------------------------------+  
mysql> SELECT REGEXP_LIKE('a', '^[a-d]');  
+----------------------------+  
| REGEXP_LIKE('a', '^[a-d]') |  
+----------------------------+  
|                          1 |  
+----------------------------+  
mysql> SELECT REGEXP_LIKE('abc', 'ABC');  
+---------------------------+  
| REGEXP_LIKE('abc', 'ABC') |  
+---------------------------+  
|                         1 |  
+---------------------------+  
mysql> SELECT REGEXP_LIKE('abc', 'ABC', 'c');  
+--------------------------------+  
| REGEXP_LIKE('abc', 'ABC', 'c') |  
+--------------------------------+  
|                              0 |  
+--------------------------------+  
mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X');  
+-----------------------------------+  
| REGEXP_REPLACE('a b c', 'b', 'X') |  
+-----------------------------------+  
| a X c                             |  
+-----------------------------------+  
mysql> SELECT REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3);  
+----------------------------------------------------+  
| REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3) |  
+----------------------------------------------------+  
| abc def X                                          |  
+----------------------------------------------------+  
mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+');  
+----------------------------------------+  
| REGEXP_SUBSTR('abc def ghi', '[a-z]+') |  
+----------------------------------------+  
| abc                                    |  
+----------------------------------------+  
mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3);  
+----------------------------------------------+  
| REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3) |  
+----------------------------------------------+  
| ghi                                          |  
+----------------------------------------------+