Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Regular Expressions

MySQL Regular Expressions

In previous chapters we learned that MySQL can use LIKE ...% for fuzzy matching.

MySQL also supports matching with other regular expressions. MySQL uses the REGEXP and RLIKE operators for regular expression matching.

If you are familiar with PHP or Perl, it is very simple to operate, because MySQL's regular expression matching is similar to those scripts.

The regular expression patterns in the following table can be applied to the REGEXP operator.

Mode Description
^ Matches the beginning position of the input string. If the RegExp object's Multiline property is set, ^ also matches the position after '\n' or '\r'.
$ Matches the ending position of the input string. If the RegExp object's Multiline property is set, $ also matches the position before '\n' or '\r'.
. Matches any single character except "\n". To match any character including '\n', use a pattern like '[.\n]'.
[...] Character set. Matches any one of the contained characters. For example, '[abc]' can match the 'a' in "plain".
[^...] Negated character set. Matches any character not contained. For example, '[^abc]' can match the 'p' in "plain".
p1|p2|p3 Matches p1 or p2 or p3. For example, 'z|food' can match "z" or "food". '(z|f)ood' matches "zood" or "food".
* Matches the preceding subexpression zero or more times. For example, zo* can match "z" as well as "zoo". * is equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, 'zo+' can match "zo" as well as "zoo", but not "z". + is equivalent to {1,}.
{n} n is a non-negative integer. Matches exactly n times. For example, 'o{2}' cannot match the 'o' in "Bob", but can match the two o's in "food".
{n,m} m and n are both non-negative integers, where n <= m. Matches at least n times and at most m times.

Character classes for regular expression matching


Pattern matching using REGEXP

REGEXP is an operator used for regular expression matching.

REGEXP is used to check whether a string matches a specified regular expression pattern. The following is the basic syntax of the REGEXP operator:

SELECT column1, column2, ...

FROM table_name

WHERE column_name REGEXP 'pattern';

Parameter Description:

Find all data in the name field that starts with 'st' as the beginning:



mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';

Find all data in the name field that starts with 'ok' as the ending:



mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';

Find all data in the name field that contains 'mar' the string:



mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';

Find all data in the name field that starts with a vowel or with 'ok' as the ending string:



mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';

Select records from the orders table where the description contains "item" followed by one or more digits.

SELECT * FROM orders WHERE order_description REGEXP 'item[0-9]+';

Use the BINARY keyword to make matching case-sensitive:



SELECT * FROM products WHERE product_name REGEXP BINARY 'apple';

Use OR for multiple matching conditions. The following selects employee records whose last name is "Smith" or "Johnson":



SELECT * FROM employees WHERE last_name REGEXP 'Smith|Johnson';

Pattern matching using RLIKE

RLIKE is an operator in MySQL used for regular expression matching, the same as REGEXP. RLIKE and REGEXP can be used interchangeably with no difference.

The following is the basic syntax for regular expression matching using RLIKE:



SELECT column1, column2, ...

FROM table_name

WHERE column_name RLIKE 'pattern';

Parameter Description:



SELECT * FROM products WHERE product_name RLIKE '^[0-9]';

The above SQL statement selects all products whose product name starts with a digit.