Previous: [MySQL Must Know and Know (5)] [Use wildcards to filter]
+++++++++++++Start line++++++++++++++++
Article Directory
1. Introduction to regular expressions
All kinds of programming languages, text editors, and operating systems support regular expressions.
Second, use MySQL regular expressions
MySQL provides preliminary support for regular expressions with the WHERE clause, allowing you to specify regular expressions and filter the data retrieved by SELECT
Only a subset of the regular expression language
MySQL supports only a small subset of most regular expression implementations
2.1 Basic character matching
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '1000'
-> ORDER BY prod_name;
analysis
This statement looks very much like a statement using LIKE. What follows REGEXP is treated as a regular expression
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '.000'
-> ORDER BY prod_name;
analysis
. Is a special character in regular expression language. It means to match any character, so 1000 and 2000 are matched and return
LIKE与REGEXP
LIKE matches the entire column. If the matched text appears in the column value, LIKE will not be found, and the corresponding row will not be returned. While REGEXP matches within the column value, if the matched text appears in the column value, REGEXP will find it and the corresponding row will be returned.
2.2 Perform OR matching
To search for one of two strings, use |
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '1000|2000'
-> ORDER BY prod_name;
2.3 Match one of several characters
By specifying a set of characters enclosed in [and]
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '[123] Ton'
-> ORDER BY prod_name;
analysis
[123]Ton is the abbreviation of [1|2|3]Ton
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '1|2|3 Ton'
-> ORDER BY prod_name;
analysis
[^123] matches anything except these characters
2.4 Matching range
Sets can be used to define one or more characters to match
The range is not limited to the complete set, the range is not necessarily just a number, [az] matches any character
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '[1-5] Ton'
-> ORDER BY prod_name;
2.5 match special characters
mysql> SELECT vend_name
-> FROM vendors
-> WHERE vend_name REGEXP '.'
-> ORDER BY vend_name;
analysis
. Matches any character, so every line is retrieved
mysql> SELECT vend_name
-> FROM vendors
-> WHERE vend_name REGEXP '\\.'
-> ORDER BY vend_name;
match\
In order to match the backslash character itself, you need to use \\
2.6 Matching multiple instances
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '\\([0-9] sticks?\\)'
-> ORDER BY prod_name;
analysis
[0-9] Match any number
sticks? Match sticks and sticks
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '[[:digit:]]{4}'
-> ORDER BY prod_name;
analysis
[:digit:] matches any digit
{4} requires the character before it to appear 4 times
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '[0-9][0-9][0-9][0-9]'
-> ORDER BY prod_name;
2.8 Locator
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name REGEXP '^[0-9\\.]'
-> ORDER BY prod_name;
analysis
^The beginning of the match string
Make REGXP behave like LIKE
The difference between LIKE and REGEXP is that LIKE matches the entire string and REGEXP matches the string. Using locators, by starting each expression with ^ and ending each expression with $, you can make REGEXP the same as LIKE
+++++++++++++End line++++++++++++++++
Next: [MySQL Must Know and Know (7)] [Create Calculated Field]