REGEXP Operator for Regular Expressions
The REGEXP operator (or its synonym RLIKE) in MySQL is used for searching and manipulating string data using regular expressions.
Regular Expression Operator ~
The ~ and ~* operators in PostgreSQL are used for searching and manipulating string data using regular expressions.
Regular expressions provide powerful capabilities for complex search patterns that are difficult to implement with the LIKE operator.
When to use regular expressions instead of LIKE?
The LIKE operator is convenient for simple search patterns, such as finding strings that start or end with a certain set of characters or contain certain substrings. However, if a more complex and flexible search is required, such as searching by multiple conditions or using special characters and ranges, regular expression operators become an indispensable tool.
Regular expression syntax
MySQL 8.1... WHERE table_field REGEXP 'pattern';
Where pattern is the regular expression that defines the search pattern.
MySQL 8.1... WHERE table_field ~ 'pattern'; -- case-sensitive ... WHERE table_field ~* 'pattern'; -- case-insensitive
Where pattern is the regular expression that defines the search pattern.
Important Considerations
-
Case insensitive
By default, regular expressions in MySQL are not case-sensitive.
For example, the expression REGEXP 'abc' will match the string abc, Abc, and ABC. -
Special characters
Some characters have special meanings in regular expressions and require escaping (e.g., ., *, +, ?, [, ], (, ), {, }, |, \).
To escape such characters, use a double backslash — \\.
-
Case sensitivity
By default, regular expressions in PostgreSQL are case-sensitive.
- The ~ operator — case-sensitive
- The ~* operator — case-insensitive
-
Special characters
Some characters have special meanings in regular expressions and require escaping (e.g., ., *, +, ?, [, ], (, ), {, }, |, \).
To escape such characters, use a single backslash — \.
Special characters and constructs
Examples with explanation
-
Get all users whose names start with "John":
MySQL 8.1SELECT * FROM Users WHERE name REGEXP '^John'
MySQL 8.1SELECT * FROM Users WHERE name ~ '^John'
This expression searches for strings starting with "John". The ^ symbol indicates the start of the string.
-
Display all school subjects whose names end with the letter "e" or "y":
MySQL 8.1SELECT * FROM Subject WHERE name REGEXP '[ey]$'
MySQL 8.1SELECT * FROM Subject WHERE name ~ '[ey]$'
In this example, [ey] defines a list of possible values for the pattern $, which defines what the string should end with.
-
Find all users whose email addresses end with "@outlook.com" or "@icloud.com":
MySQL 8.1SELECT * FROM Users WHERE email REGEXP '@(outlook.com|icloud.com)$'
MySQL 8.1SELECT * FROM Users WHERE email ~ '@(outlook\.com|icloud\.com)$'
Here, $ is used to indicate the end of the string and | is used to specify multiple options.
-
Find all users whose phone numbers do not contain the digits "2" and "8":
MySQL 8.1SELECT * FROM Users WHERE phone_number REGEXP '^[^28]*$'
MySQL 8.1SELECT * FROM Users WHERE phone_number ~ '^[^28]*$'
In this example, the symbol [^28] represents any character except "2" and "8", and * means any number of such characters. The ^ and $ symbols indicate the start and end of the string respectively, ensuring that the entire string matches the pattern.
-
Find all users whose phone number starts with «+7»
MySQL 8.1SELECT name, phone_number FROM Users WHERE phone_number REGEXP '^\\+7'
MySQL 8.1SELECT name, phone_number FROM Users WHERE phone_number ~ '^\+7'
In this example, ^ denotes the beginning of a string. This means we are looking for strings that start with a specific pattern.
Since + is a special character in regular expressions, it needs to be escaped with a double backslash (\\) so that it is treated as the literal + character. As a result, \\+ matches the + sign in the string.
Since + is a special character in regular expressions, it needs to be escaped with a single backslash (\) so that it is treated as the literal + character. As a result, \+ matches the + sign in the string.