在字符串的开始处进行匹配:mysql
mysql> select 'wqh' regexp '^w';正则表达式
+-------------------+sql
| 'wqh' regexp '^w' |ide
+-------------------+regexp
| 1 |ci
+-------------------+字符串
1 row in set (0.00 sec)it
mysql> class
2.在字符串的末尾处进行匹配:email
mysql> select 'wqh' regexp 'h$';
+-------------------+
| 'wqh' regexp 'h$' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql>
3.匹配任意单个字符,包括换行符:
mysql> select 'abcd' regexp '.c', 'abcd' regexp '.f';
+--------------------+--------------------+
| 'abcd' regexp '.c' | 'abcd' regexp '.f' |
+--------------------+--------------------+
| 1 | 0 |
+--------------------+--------------------+
1 row in set (0.00 sec)
mysql>
4.匹配括号内的任意字符:
mysql> select 'abcdefh' regexp '[fhk]';
+--------------------------+
| 'abcdefh' regexp '[fhk]' |
+--------------------------+
| 1 |
+--------------------------+
1 row in set (0.00 sec)
mysql>
实例:
mysql> select first_name,email from customer where email regexp "@163[,.]com$";
+------------+---------------+
| first_name | email |
+------------+---------------+
| 11 | bj@163.com |
| 11 | bsssj@163.com |
+------------+---------------+
2 rows in set (0.00 sec)
mysql> select first_name,email from customer where email regexp ".*@163[,.]com$";
+------------+---------------+
| first_name | email |
+------------+---------------+
| 11 | bj@163.com |
| 11 | bsssj@163.com |
+------------+---------------+
2 rows in set (0.00 sec)
mysql> select first_name,email from customer where email regexp ".*@163.com$";
+------------+---------------+
| first_name | email |
+------------+---------------+
| 11 | bj@163.com |
| 11 | bsssj@163.com |
+------------+---------------+
2 rows in set (0.00 sec)
mysql> select first_name,email from customer where email regexp "@163.com$";
+------------+---------------+
| first_name | email |
+------------+---------------+
| 11 | bj@163.com |
| 11 | bsssj@163.com |
+------------+---------------+
2 rows in set (0.00 sec)
mysql>
若是不实用正则表达式:
mysql> select first_name,email from customer where email like "%@163%.com" or email like "%@163%,com" ;
+------------+---------------+
| first_name | email |
+------------+---------------+
| 11 | bj@163.com |
| 11 | bsssj@163.com |
+------------+---------------+
2 rows in set (0.00 sec)
mysql>