mysql> SELECT 'auuuuc' REGEXP 'au{3,}c';
+---------------------------+
| 'auuuuc' REGEXP 'au{3,}c' |
+---------------------------+
| 1 |
+---------------------------+
row in set (0.00 sec)
mysql> SELECT 'auuuuc' REGEXP 'au{4,}c';
+---------------------------+
| 'auuuuc' REGEXP 'au{4,}c' |
+---------------------------+
| 1 |
+---------------------------+
row in set (0.00 sec)
mysql> SELECT 'auuuuc' REGEXP 'au{5,}c';
+---------------------------+
| 'auuuuc' REGEXP 'au{5,}c' |
+---------------------------+
| 0 |
+---------------------------+
row in set (0.00 sec)
a{m,n}
匹配m到n个a,包含m和n
mysql> SELECT 'auuuuc' REGEXP 'au{3,5}c';
+----------------------------+
| 'auuuuc' REGEXP 'au{3,5}c' |
+----------------------------+
| 1 |
+----------------------------+
row in set (0.00 sec)
mysql> SELECT 'auuuuc' REGEXP 'au{4,5}c';
+----------------------------+
| 'auuuuc' REGEXP 'au{4,5}c' |
+----------------------------+
| 1 |
+----------------------------+
row in set (0.00 sec)
mysql> SELECT 'auuuuc' REGEXP 'au{5,10}c';
+-----------------------------+
| 'auuuuc' REGEXP 'au{5,10}c' |
+-----------------------------+
| 0 |
+-----------------------------+
row in set (0.00 sec)
(abc)
将abc作为一个序列匹配,不用括号括起来都是用单个字符去匹配,如果要把多个字符作为一个整体去匹配就需要用到括号,所以括号适合上面的所有情况。
mysql> SELECT 'xababy' REGEXP 'x(abab)y';
+----------------------------+
| 'xababy' REGEXP 'x(abab)y' |
+----------------------------+
| 1 |
+----------------------------+
row in set (0.00 sec)
mysql> SELECT 'xababy' REGEXP 'x(ab)*y';
+---------------------------+
| 'xababy' REGEXP 'x(ab)*y' |
+---------------------------+
| 1 |
+---------------------------+
row in set (0.00 sec)
mysql> SELECT 'xababy' REGEXP 'x(ab){1,2}y';
+-------------------------------+
| 'xababy' REGEXP 'x(ab){1,2}y' |
+-------------------------------+
| 1 |
+-------------------------------+
row in set (0.00 sec)
总结
特别要注意最后的()的命令,如果不使用()那么所有的都是指单个字符去做匹配,如果需要使用多个字符作为一个整体去匹配,就需要将多个字符使用()给括起来
1.使用REGEXP和NOT REGEXP操作符(类似LIKE和NOT LIKE);
2.REGEXP默认也是不区分大小写,可以使用BINARY关键词强制区分大小写; WHERE NAME REGEXP BINARY ‘^[A-Z]’;
3.REGEXP默认是部分匹配原则,即有一个匹配上则返回真。例如:SELECT 'Abc' REGEXP BINARY '[A-Z]';
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx