SELECT 'AaYYY1231' REGEXP '[A-Z,0-9]+';
结果:
1
为什么它返回1,当我希望它返回0? 从哪里找到'a'的匹配?
答案 0 :(得分:3)
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
“REGEXP不区分大小写,除非与二进制字符串一起使用。”
来自doc的一个例子:
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
+----------------+-----------------------+
| 'a' REGEXP 'A' | 'a' REGEXP BINARY 'A' |
+----------------+-----------------------+
| 1 | 0 |
+----------------+-----------------------+
此外,它并不贪心,因此无法确保整个字符串匹配。修改上面的示例会产生:
mysql> SELECT 'a' REGEXP BINARY '[A-Z0-9]+', 'Aa' REGEXP BINARY '[A-Z0-9]+';
+-------------------------------+--------------------------------+
| 'a' REGEXP BINARY '[A-Z0-9]+' | 'Aa' REGEXP BINARY '[A-Z0-9]+' |
+-------------------------------+--------------------------------+
| 0 | 1 |
+-------------------------------+--------------------------------+
为了解决这个问题,您可以为行的开头和结尾添加标记:
mysql> SELECT 'Aa' REGEXP BINARY '^[A-Z0-9]+$';
+----------------------------------+
| 'Aa' REGEXP BINARY '^[A-Z0-9]+$' |
+----------------------------------+
| 0 |
+----------------------------------+
这导致你想要的最终答案:
mysql> SELECT 'AaYYY1231' REGEXP BINARY '^[A-Z,0-9]+$';
+------------------------------------------+
| 'AaYYY1231' REGEXP BINARY '^[A-Z,0-9]+$' |
+------------------------------------------+
| 0 |
+------------------------------------------+