正則表達式用來描述或者匹配符合規則的字符串。它的用法和like比較類似,可是它又比like更強大,可以實現一些很特殊的規則匹配;正則表達式須要使用REGEXP命令,匹配上返回"1"匹配不上返回"0",默認不加條件REGEXP至關於like '%%'。在前面加上NOT至關於NOT LIKE。mysql
命令正則表達式 |
說明sql |
^post |
在字符的開啓處進行匹配測試 |
$spa |
在字符的末尾處進行匹配code |
.blog |
匹配任何字符(包括回車和新行)ci |
[….]字符串 |
匹配括號內的任意單個字符 |
[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
|
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
'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]';