正则表达式用来描述或者匹配符合规则的字符串。它的用法和like比较相似,但是它又比like更强大,能够实现一些很特殊的规则匹配;正则表达式需要使用REGEXP命令,匹配上返回"1"匹配不上返回"0",默认不加条件REGEXP相当于like ‘%%‘。在前面加上NOT相当于NOT LIKE。
命令 |
说明 |
^ |
在字符的开启处进行匹配 |
$ |
在字符的末尾处进行匹配 |
. |
匹配任何字符(包括回车和新行) |
[….] |
匹配括号内的任意单个字符 |
[m-n] |
匹配m到n之间的任意单个字符,例如[0-9],[a-z],[A-Z] |
[^..] |
不能匹配括号内的任意单个字符 |
a* |
匹配0个或多个a,包括空,可以作为占位符使用. |
a+ |
匹配一个或多个a,不包括空 |
a? |
匹配一个或0个a |
a1| a2 |
匹配a1或a2 |
a{m} |
匹配m个a |
a{m,} |
匹配m个或者更多个a |
a{m,n} |
匹配m到n个a |
a{,n} |
匹配0到n个a |
(….) |
将模式元素组成单一元素,例如(do)*意思是匹配0个多或多个do
|
二 使用测试
^
在字符串开始处进行匹配
1
2
3
4
5
6
7
|
+-------------------+ | ‘abc‘ REGEXP ‘^a‘ | +-------------------+ | 1 | +-------------------+ row in set (0.00 sec) |
$
在字符串末尾开始匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> SELECT ‘abc‘ REGEXP ‘a$‘ ; +-------------------+ | ‘abc‘ REGEXP ‘a$‘ | +-------------------+ | 0 | +-------------------+ row in set (0.01 sec) mysql> SELECT ‘abc‘ REGEXP ‘c$‘ ; +-------------------+ | ‘abc‘ REGEXP ‘c$‘ | +-------------------+ | 1 | +-------------------+ row in set (0.00 sec) |
.
匹配任意字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
mysql> SELECT ‘abc‘ REGEXP ‘.a‘ ; +-------------------+ | ‘abc‘ REGEXP ‘.a‘ | +-------------------+ | 0 | +-------------------+ row in set (0.00 sec) mysql> SELECT ‘abc‘ REGEXP ‘.b‘ ; +-------------------+ | ‘abc‘ REGEXP ‘.b‘ | +-------------------+ | 1 | +-------------------+ row in set (0.00 sec) mysql> SELECT ‘abc‘ REGEXP ‘.c‘ ; +-------------------+ | ‘abc‘ REGEXP ‘.c‘ | +-------------------+ | 1 | +-------------------+ row in set (0.00 sec) mysql> SELECT ‘abc‘ REGEXP ‘a.‘ ; +-------------------+ | ‘abc‘ REGEXP ‘a.‘ | +-------------------+ | 1 | +-------------------+ row in set (0.00 sec) |
[...]
匹配括号内的任意单个字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> SELECT ‘abc‘ REGEXP ‘[xyz]‘ ; +----------------------+ | ‘abc‘ REGEXP ‘[xyz]‘ | +----------------------+ | 0 | +----------------------+ row in set (0.00 sec) mysql> SELECT ‘abc‘ REGEXP ‘[xaz]‘ ; +----------------------+ | ‘abc‘ REGEXP ‘[xaz]‘ | +----------------------+ | 1 | +----------------------+ row in set (0.00 sec) |
[^...]
注意^符合只有在[]内才是取反的意思,在别的地方都是表示开始处匹配
注意:REGEXP 前的匹配字符作为一个整体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
mysql> SELECT ‘a‘ REGEXP ‘[^abc]‘ ; +---------------------+ | ‘a‘ REGEXP ‘[^abc]‘ | +---------------------+ | 0 | +---------------------+ row in set (0.00 sec) mysql> SELECT ‘x‘ REGEXP ‘[^abc]‘ ; +---------------------+ | ‘x‘ REGEXP ‘[^abc]‘ | +---------------------+ | 1 | +---------------------+ row in set (0.00 sec) mysql> SELECT ‘abc‘ REGEXP ‘[^a]‘ ; +---------------------+ | ‘abc‘ REGEXP ‘[^a]‘ | +---------------------+ | 1 | +---------------------+ row in set (0.00 sec) |
注意: ‘abc‘作为一个整体,所以它匹配不了a
a*
匹配0个或多个a,包括空字符串。 可以作为占位符使用.有没有指定字符都可以匹配到数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
mysql> SELECT ‘stab‘ REGEXP ‘.ta*b‘ ; +-----------------------+ | ‘stab‘ REGEXP ‘.ta*b‘ | +-----------------------+ | 1 | +-----------------------+ row in set (0.00 sec) mysql> SELECT ‘stb‘ REGEXP ‘.ta*b‘ ; +----------------------+ | ‘stb‘ REGEXP ‘.ta*b‘ | +----------------------+ | 1 | +----------------------+ mysql> SELECT ‘‘ REGEXP ‘a*‘ ; +----------------+ | ‘‘ REGEXP ‘a*‘ | +----------------+ | 1 | +----------------+ 1 row in set (0.00 sec) |
a+
匹配1个或者多个a,但是不包括空字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> SELECT ‘stab‘ REGEXP ‘.ta+b‘ ; +-----------------------+ | ‘stab‘ REGEXP ‘.ta+b‘ | +-----------------------+ | 1 | +-----------------------+ row in set (0.00 sec) mysql> SELECT ‘stb‘ REGEXP ‘.ta+b‘ ; +----------------------+ | ‘stb‘ REGEXP ‘.ta+b‘ | +----------------------+ | 0 | +----------------------+ row in set (0.00 sec) |
a?
匹配0个或者1个a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
mysql> SELECT ‘stb‘ REGEXP ‘.ta?b‘ ; +----------------------+ | ‘stb‘ REGEXP ‘.ta?b‘ | +----------------------+ | 1 | +----------------------+ row in set (0.00 sec) mysql> SELECT ‘stab‘ REGEXP ‘.ta?b‘ ; +-----------------------+ | ‘stab‘ REGEXP ‘.ta?b‘ | +-----------------------+ | 1 | +-----------------------+ row in set (0.00 sec) mysql> SELECT ‘staab‘ REGEXP ‘.ta?b‘ ; +------------------------+ | ‘staab‘ REGEXP ‘.ta?b‘ | +------------------------+ | 0 | +------------------------+ row in set (0.00 sec) |
a1|a2
匹配a1或者a2,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
mysql> SELECT ‘a‘ REGEXP ‘a|b‘ ; +------------------+ | ‘a‘ REGEXP ‘a|b‘ | +------------------+ | 1 | +------------------+ row in set (0.00 sec) mysql> SELECT ‘b‘ REGEXP ‘a|b‘ ; +------------------+ | ‘b‘ REGEXP ‘a|b‘ | +------------------+ | 1 | +------------------+ row in set (0.00 sec) mysql> SELECT ‘b‘ REGEXP ‘^(a|b)‘ ; +---------------------+ | ‘b‘ REGEXP ‘^(a|b)‘ | +---------------------+ | 1 | +---------------------+ row in set (0.00 sec) mysql> SELECT ‘a‘ REGEXP ‘^(a|b)‘ ; +---------------------+ | ‘a‘ REGEXP ‘^(a|b)‘ | +---------------------+ | 1 | +---------------------+ row in set (0.00 sec) mysql> SELECT ‘c‘ REGEXP ‘^(a|b)‘ ; +---------------------+ | ‘c‘ REGEXP ‘^(a|b)‘ | +---------------------+ | 0 | +---------------------+ row in set (0.00 sec) |
注意:^符合只有在[]内才是取反的意思,在别的地方都是表示开始处匹配
a{m}
匹配m个a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> SELECT ‘auuuuc‘ REGEXP ‘au{4}c‘ ; +--------------------------+ | ‘auuuuc‘ REGEXP ‘au{4}c‘ | +--------------------------+ | 1 | +--------------------------+ row in set (0.00 sec) mysql> SELECT ‘auuuuc‘ REGEXP ‘au{3}c‘ ; +--------------------------+ | ‘auuuuc‘ REGEXP ‘au{3}c‘ | +--------------------------+ | 0 | +--------------------------+ row in set (0.00 sec) |
a{m,}
匹配m个或者更多个a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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作为一个序列匹配,不用括号括起来都是用单个字符去匹配,如果要把多个字符作为一个整体去匹配就需要用到括号,所以括号适合上面的所有情况。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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]‘;