SELECT 'AaYYY1231' REGEXP '[A-Z,0-9]+';
Result:
1
Why is it returning 1, when I expect it to return 0 ? From where is is it finding match for 'a' ?
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
“REGEXP 不区分大小写,除非与二进制字符串一起使用。”
文档中的一个例子:
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 |
+------------------------------------------+