1、正則表達式介紹
正則表達式用來匹配文本的特殊的串(字符集合)。
2、使用MySQL正則表達式mysql
一、MySQL僅僅支持多數正則表達式實現的一個很小的子集
二、LIKE匹配整個列值;而REGEXP匹配列值的子串
git
若是被匹配的文本在列值中出現,REGEXP將會找到它,相應的行將被返回。
mysql>select * from students;
+--------+-----------+
|number | name1|
+--------+-----------+
|1 | space|
|2 | warmspace |
+--------+-----------+正則表達式
mysql>select name1 from students where name1 like 'space'; 全名匹配
+-------+
|name1 |
+-------+
|space |
+-------+
mysql>select name1 from students where name1 REGEXP 'space'; 子串匹配
+-----------+
|name1|
+-----------+
|space|
|warmspace |
+-----------+sql
三、默認MySQL中的正則表達式匹配不區分大小寫,可以使用BINARY關鍵字區分大小寫
mysql> select name1 from students where name1 REGEXP 'SPACE';
+-----------+
| name1 |
+-----------+
| space |
| warmspace |
+-----------+數據庫
mysql> select name1 from students where name1 REGEXP BINARY 'SPACE';
Empty set (0.00 sec)api
四、進行OR匹配:爲搜索兩個串之一,使用|
mysql> select * from students;
+--------+-----------+------+
| number | name1 | age |
+--------+-----------+------+
| 1 | space | 15 |
| 2 | warmspace | 34 |
| 3 | summerspace | 28 |
+--------+-----------+------+
2 rows in set (0.00 sec)
mysql> select * from students where age REGEXP '1|4';
+--------+-----------+------+
| number | name1 | age |
+--------+-----------+------+
| 1 | space | 15 |
| 2 | warmspace | 34 |
+--------+-----------+------+ide
五、匹配幾個字符之一:匹配任何單一字符
[]是另外一種形式的OR語句。'005-[123]'與’005-[1|2|3]’是等價的。
mysql> select * from students;
+--------+-------------+------+-----------------+
| number | name1 | age | id |
+--------+-------------+------+-----------------+
| 1 | space | 15 | 101-02-01-005-2 |
| 2 | warmspace | 34 | 101-02-01-005-1 |
| 3 | summerspace | 28 | 101-02-01-005-3 |
| 4 | abc | 20 | 101-02-01-005-4 |
+--------+-------------+------+-----------------+
mysql> select name1,id from students where id REGEXP '005-[123]';
+-------------+-----------------+
| name1 | id |
+-------------+-----------------+
| space | 101-02-01-005-2 |
| warmspace | 101-02-01-005-1 |
| summerspace | 101-02-01-005-3 |
+-------------+-----------------+
匹配除給定字符外的字符:
mysql> select name1,id from students where id REGEXP '005-[^123]';
+-------+-----------------+
| name1 | id |
+-------+-----------------+
| abc | 101-02-01-005-4 |
+-------+-----------------+測試
六、可以使用-來定義一個範圍
如[1-9],[a-b]。
mysql> select name1,id from students where id REGEXP '005-[1-3]';
+-------------+-----------------+
| name1 | id |
+-------------+-----------------+
| space | 101-02-01-005-2 |
| warmspace | 101-02-01-005-1 |
| summerspace | 101-02-01-005-3 |
+-------------+-----------------+spa
七、爲了匹配特殊字符,必須用\\爲前導
例如\\-,\\.等。這種處理即轉義(escaping)。多數正則表達式實現使用單個反斜槓轉義特殊字符,但MYSQL要求兩個(MYSQL本身解釋一個,正則表達式庫解釋另外一個)。\\也用來引用元字符(具備特殊含義的字符),以下表:regexp
元字符 |
說明 |
\\f |
換頁 |
換行 |
|
回車 |
|
製表 |
|
\\v |
縱向製表 |
八、爲了更方便工做,能夠使用預約義的字符集,稱爲字符類(character class)
mysql> select name1,age from students where age REGEXP '[[:digit:]]';
+-------------+------+
| name1 | age |
+-------------+------+
| space | 15 |
| warmspace | 34 |
| summerspace | 28 |
| abc | 20 |
+-------------+------+
類 |
說明 |
[:alnum:] |
任意字母和數字(同[a-zA-Z0-9]) |
[:alpha:] |
任意字符(同[a-zA-Z]) |
[:blank:] |
空格和製表(同[\\t]) |
[:cntrl:] |
ASCII控制字符(ASCII 0到31和127) |
[:digit:] |
任意數字(同[0-9]) |
[:graph:] |
與[:print:]相同,但不包括空格 |
[:lower:] |
任意小寫字母(同[a-z]) |
[:print:] |
任意可打印字符 |
[:punct:] |
既不在[:alnum:]又不在[:cntrl:]中的任意字符 |
[:space:] |
包括空格在內的任意空白字符(同[\\f\\n\\r\\t\\v]) |
[:upper:] |
任意大寫字母(同[A-Z]) |
[:xdigit:] |
任意十六進制數字(同[a-fA-F0-9]) |
九、使用正則表達式重複元字符匹配多個實例
mysql> select * from students;
+--------+-------------+------+-----------------+
| number | name1 | age | id |
+--------+-------------+------+-----------------+
| 1 | space | 15 | 101-02-01-005-2 |
| 2 | warmspace | 34 | 101-02-01-005-1 |
| 3 | summerspace | 28 | 101-02-01-005-3 |
| 4 | abc | 20 | 101-02-01-005-4 |
+--------+-------------+------+-----------------+
mysql> select * from students where name1 REGEXP 'm{2}';
+--------+-------------+------+-----------------+
| number | name1 | age | id |
+--------+-------------+------+-----------------+
| 3 | summerspace | 28 | 101-02-01-005-3 |
+--------+-------------+------+-----------------+
元字符 |
說明 |
* |
0個或多個匹配 |
+ |
1個或多個匹配(等於{1,}) |
? |
0個或1個匹配(等於{0,1}) |
{n} |
指定數目的匹配 |
{n,} |
很多於指定數目的匹配 |
{n,m} |
匹配數目的範圍(m不超過255) |
十、定位符(爲了匹配特定位置的文本)
mysql> select * from students where name1 REGEXP 'c$';
+--------+-------+------+-----------------+
| number | name1 | age | id |
+--------+-------+------+-----------------+
| 4 | abc | 20 | 101-02-01-005-4 |
+--------+-------+------+-----------------+
元字符 |
說明 |
^ |
文本的開始 |
$ |
文本的結尾 |
[[:<:]] |
詞的開始 |
[[:>:]] |
詞的結尾 |
十一、簡單的正則表達式測試:能夠在不使用數據庫表的狀況下用SELECT來測試正則表達式REGEXP檢查老是返回0或1(匹配)。mysql> select 'hello' regexp '[0-9]';+------------------------+| 'hello' regexp '[0-9]' |+------------------------+| 0 |+------------------------+