Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

MySQL正则匹配字符类

我不懂MySQL中匹配字符类到底怎么理解?[:upper:]表示任意大写字母,同[A-Z],通过下面这一句得到出现过大写字母的prod_name:

SELECT prod_name FROM Products WHERE prod_name REGEXP '[:upper:]';

使用[[:upper:]]则会全部输出。

同样的道理,在使用数字字符类[:digit:]时,需要双括号才能匹配到出现过数字的prod_name:

SELECT prod_name FROM Products WHERE prod_name REGEXP '[[:digit:]]';

而使用单括号则不行,请问这是为什么?该怎么理解?是否需要指定正则匹配出现的次数?如何指定?

image

mysql> SELECT prod_name FROM Products;
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Detonator      |
| Bird seed      |
| Carrots        |
| Fuses          |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
14 rows in set (0.00 sec)

mysql> SELECT prod_name FROM Products WHERE prod_name REGEXP '[:upper:]';
+--------------+
| prod_name    |
+--------------+
| Detonator    |
| Bird seed    |
| Carrots      |
| Fuses        |
| JetPack 1000 |
| JetPack 2000 |
| Safe         |
+--------------+
7 rows in set (0.01 sec)

mysql> SELECT prod_name FROM Products WHERE prod_name REGEXP '[[:upper:]]';
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Detonator      |
| Bird seed      |
| Carrots        |
| Fuses          |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
14 rows in set (0.01 sec)

mysql> SELECT prod_name FROM Products WHERE prod_name REGEXP '[[:digit:]]';
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| JetPack 1000   |
| JetPack 2000   |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
7 rows in set (0.00 sec)

mysql> SELECT prod_name FROM Products WHERE prod_name REGEXP '[:digit:]';
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Detonator      |
| Bird seed      |
| Carrots        |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
12 rows in set (0.00 sec)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...