MySQL正則表達式匹配查詢 一、正則表達式介紹
? 在過濾的時候允許使用匹配、比較和通配符尋找數據。對于基本的過濾,這些可能就足夠了。但是隨著過濾條件復雜性的增加,where子句本身的復雜性也有必要增加。
? 這里簡單介紹一下使用正則表達式匹配搜索。所有種類的程序設計語言、文本編輯器、操作系統都支持正則表達式。很多程序員都將正則表達式作為自己的技能。熟練使用正則表達式,可以幫助我們減少很多的麻煩。
? 正則表達式用正則表達式語言來建立,正則表達式語言是用來完成過濾、匹配類工作的一種特殊語言。與其他語言一樣,它用于自己的特殊的語法和指令。
二、使用正則表達式
? 正則表達式的作用是匹配文本,將一個模式與一個文本串進行比較,根據自定義的模式,過濾出你需要的數據。MySQL的正則表達式只是正則表達式的一個子集。
測試數據
mysql> select * from regexp_test;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)1.基本字符匹配
使用like匹配name為xiao的用戶,需要借助通配符%
mysql> select * from regexp_test where name like 'xiao%';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.01 sec)mysql> explain select * from regexp_test where name like 'xiao%';使用regexp匹配name為xiao的用戶,無需借助任何通配符
regexp在列值內進行匹配,如果被匹配的文本在列值中出現,regexp將會找到他,相應的行將被返回。
regexp正則表達式匹配不區分大小寫
mysql> select * from regexp_test where name regexp 'xiao';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.00 sec)
2.OR匹配
使用or匹配多個符合條件的數據
mysql> select * from regexp_test where id = 1 or id = 2;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用regexp匹配多個符合條件的數據
使用regexp的|功能類似于在select中使用or
mysql> select * from regexp_test where id regexp '1|2';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)3.模糊匹配
使用or模糊匹配
mysql> select * from regexp_test where id = 1 or id = 2 or id = 8;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用|匹配其中符合條件的
mysql> select * from regexp_test where id regexp '1|2|8';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用[]匹配符合條件
mysql> select * from regexp_test where id regexp '[128]';+------+----------+| id | name |
+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)4.匹配范圍
[123456789]即為匹配到123456789這個集合
[1-9]即為匹配到123456789這個集合
[a-z]匹配任意字母
mysql> select * from regexp_test where name regexp '[a-z]';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)mysql> select * from regexp_test where id regexp '[0-9]';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)5.匹配特殊字符
.和-在正則表達式中是特殊字符,需要使用兩個雙引號//轉義,例如//-表示查找-,例如//.表示查找.
原字符說明
//f
換頁
//n
換行
//r
回車
/
制表
//v
縱向制表
至于為什么要有兩個反斜杠,MySQL要求需要兩個,一個是MySQL自身需要,一個正則表達式需要

## 模擬插入一條帶.的數據mysql> insert into regexp_test values(8,'z.y');## 因為.匹配任意字符,所以會把所有數據都匹配到mysql> select * from regexp_test where name regexp '.';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 || 8 | z.y |+------+----------+8 rows in set (0.00 sec)## 當使用兩個反斜杠轉義后,查詢出的結果和我們的期望一致mysql> select * from regexp_test where name regexp '//.';+------+------+| id | name |+------+------+| 8 | z.y |+------+------+1 row in set (0.00 sec)6. 匹配字符類
存在找出經常使用使用數字、所有字母字符或所有數字字母字符的匹配。為方便使用,可以采取預定義的字符集,稱為字符類。
類說明
[:alnum:]
任意字符和數字,同[a-zA-Z0-9]
[:alpha:]
任意字符,同[a-zA-Z]
[:blank:]
空格和制表,同[/]
[:cntrl:]
ASCAII控制字符mysql 正則匹配中文,ASCAII 0 到31和127
[:digit:]
任意數字,同[0-9]
[:graph:]
與[:print:]相同,但不包括空格
[:lower:]
任意小寫字母,同[a-z]
[:print:]
任意可打印字符
[:punct:]
既不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:]
包括空格在內的任意空白字符,同[//f//n//r///v]
[:upper:]

任意大寫字母,同[A-Z]
[:xdigit:]
任意十六進制數字,同[a-fA-F0-9]
7.匹配多個實例
? 目前為止使用的所有正則表達式都嘗試匹配單次出現。如果存在一個匹配,該行被檢索出來,如果不存在mysql 正則匹配中文,檢索不出任何行。但有時需要對匹配的數目進行更強的控制。例如,你可能需要尋找所有的數,不管數中包含多少數字,或者你可能想尋找一個單詞并且能夠適應一個跟隨的字符,等。
元字符說明
*
0個或多個匹配
+
1個或多個匹配
?
?匹配它前面的任何字符的0次活1次出現
{n}
數目的匹配
{n,}
不少于數目的匹配
{n,m}
匹配數目的范圍(m不超過255)
例一:匹配到滿足xiaoh和xiao的數據
mysql> select * from regexp_test where name regexp 'xiaoh?';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.01 sec)例二 :仔細觀察{4}和{1}匹配到數據的不同點
## 匹配4個連續小寫字母mysql> select * from regexp_test where name regexp '[a-z]{4}';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)## 匹配1個連續小寫字母mysql> select * from regexp_test where name regexp '[a-z]{1}';+------+----------+| id | name |+------+----------+| 1 | xiaoming |
| 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 || 8 | z.y |+------+----------+8 rows in set (0.00 sec)8.定位符
? 以上介紹中都是匹配到一個字符串中任意位置的文本。為了匹配特定位置的文本,可以參考使用定位符:
元字符說明
^
文本開始
$
文本結尾
[[::]]
詞的結尾
notes:特別需要注意的是,當在`[]`內的時候代表的是否定該集合,當在[]外的時候代表的是文本開始
案例一:匹配以數字結尾
mysql> select * from regexp_test where name regexp '[a-z][0-9]$';+------+--------+| id | name |+------+--------+| 7 | liliu9 |+------+--------+1 row in set (0.01 sec)案例二:匹配以數字開頭
mysql> select * from regexp_test where name regexp '^[0-9][a-z]';+------+-------+| id | name |+------+-------+| 9 | 1zbc || 10 | 1qwr2 |+------+-------+2 rows in set (0.00 sec)案例三:匹配以數字開頭,以數字結尾
mysql> select * from regexp_test where name regexp '^[0-9][a-z]*[0-9]$';+------+-------+| id | name |+------+-------+| 10 | 1qwr2 |+------+-------+1 row in set (0.00 sec)