1、在Mysql中REGEXP后所跟的东西作为正则表达式处理。
如下,选择lname列中带有m的全部行,即匹配所有字符,这与LIKE的用法非常相似,但是其功能远远不止这些。
mysql> SELECT lname
-> FROM employee
-> WHERE lname REGEXP 'm';
+----------+
| lname |
+----------+
| Smith |
| Fleming |
| Grossman |
| Jameson |
| Mason |
| Portman |
| Markham |
| Tulman |
+----------+
8 rows in set (0.00 sec)
2、利用‘|’这个符号,即或,可以实现多种匹配,如下提取带m或者带n符号的lname
mysql> SELECT lname
-> FROM employee
-> WHERE lname REGEXP 'm|n';
+-----------+
| lname |
+-----------+
| Smith |
| Hawthorne |
| Gooding |
| Fleming |
| Grossman |
| Jameson |
| Mason |
| Portman |
| Markham |
| Tulman |
+-----------+
10 rows in set (0.00 sec)
3、利用[ ]来匹配任意字符,如[aos],表示包括a或者o或者s中的所有的行。这个地方如果再加上定位符^(文本的开始),就会查询出文本开始带有a或者0或者s的行。详见8.
mysql> SELECT lname
-> FROM employee
-> WHERE lname REGEXP '[aos]';
+-----------+
| lname |
+-----------+
| Smith |
| Barker |
| Hawthorne |
| Gooding |
| Parker |
| Grossman |
| Roberts |
| Jameson |
| Blake |
| Mason |
| Portman |
| Markham |
| Fowler |
| Tulman |
+-----------+
14 rows in set (0.01 sec)
4、匹配范围,[0123456789]或者[0-9]匹配的是从0到9的范围,而[a-z]匹配任意字母符号。
mysql> SELECT fname
-> FROM employee
-> WHERE fname REGEXP '[b-e]';
+---------+
| fname |
+---------+
| Michael |
| Robert |
| Helen |
| Chris |
| Jane |
| Cindy |
| Theresa |
| Beth |
| Rick |
+---------+
9 rows in set (0.00 sec)
5、匹配特殊字符
mysql> SELECT *
-> FROM student
-> WHERE lname REGEXP '\\.';
+------------+----------+-------+------------+--------+
| std_id | fname | lname | name | gender |
+------------+----------+-------+------------+--------+
| 2013020839 | huaifeng | lv. | lvhuaifeng | M |
+------------+----------+-------+------------+--------+
1 row in set (0.00 sec)
代码
[:a;num:] 任意字母和数字(同 [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\\t\\r\\v])
[:upper:] 任意大小字母(同 [A-Z])
[:xdigit:] 任意十六进制数字(同 [a-fA-F0-9])
7、匹配多个实例
代码
元字符 说明
* 0个或多个匹配
+ 1个或多个匹配(等于 {1, })
? 0个或1个匹配(等于 {0, 1})
{n} 指定数目的匹配
{n, } 不少于指定数目的匹配
{n ,m} 匹配数目的范围(m不超过255)
例子为,在一个数字后面加上?,对?前面的元素进行匹配,即表示有0个或者1个该元素将被选出。
mysql> SELECT *
-> FROM employee
-> WHERE start_date REGEXP '2000-1?';
+--------+--------+---------+------------+----------+
| emp_id | fname | lname | start_date | end_date |
+--------+--------+---------+------------+----------+
| 3 | Robert | Tyler | 2000-02-09 | NULL |
| 11 | Thomas | Ziegler | 2000-10-23 | NULL |
| 13 | John | Blake | 2000-05-11 | NULL |
+--------+--------+---------+------------+----------+
3 rows in set (0.00 sec)
如果换为+号,则表示有1个,或者多个进行匹配,则上的选择结果只会变成一行。
mysql> SELECT *
-> FROM employee
-> WHERE start_date REGEXP '2000-1+';
+--------+--------+---------+------------+----------+-
| emp_id | fname | lname | start_date | end_date |
+--------+--------+---------+------------+----------+-
| 11 | Thomas | Ziegler | 2000-10-23 | NULL |
+--------+--------+---------+------------+----------+-
1 row in set (0.00 sec)
8、定位符
^ 文本的开始
$ 文本的末尾
[[:<:]] 词的开始
[[:>:]] 词的结尾
mysql> SELECT *
-> from student;
+------------+----------+-------+------------+--------+
| std_id | fname | lname | name | gender |
+------------+----------+-------+------------+--------+
| 2013020839 | huaifeng | lv. | lvhuaifeng | M |
| 2013020840 | shuo | liu | liushuo | M |
| 2013020851 | na | liu | liuna | F |
+------------+----------+-------+------------+--------+
3 rows in set (0.18 sec)
mysql> SELECT *
-> FROM student
-> WHERE fname REGEXP '^[hsa]';
+------------+----------+-------+------------+--------+
| std_id | fname | lname | name | gender |
+------------+----------+-------+------------+--------+
| 2013020839 | huaifeng | lv. | lvhuaifeng | M |
| 2013020840 | shuo | liu | liushuo | M |
+------------+----------+-------+------------+--------+
2 rows in set (0.00 sec)