我在这方面做了很多努力。
在MySQL中,我想查询一列以两位数字结尾的字符串。我不在乎之前的其他内容,只要最后一个字符两个数字前面至少有一个非数字
例如,这些字符串应匹配:
"Nov. 12th, 60"
"34 Bar 56"
"Foo-BAR-01"
"33-44-55"
"-------88"
"99"
当我这样做时:
SELECT <string> REGEXP <pattern>
现在,<pattern>
位是我需要帮助的。
感谢。
答案 0 :(得分:2)
SELECT * FROM mytable WHERE mycolumn REGEXP "^.*[^0-9][0-9]{2}$";
正则表达式解释:
^.*[^0-9][0-9]{2}$
Assert position at the beginning of the string «^»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «*»
Match any single character that is NOT present in the list below and that is NOT a line break character «[^0-9]»
A character in the range between “0” and “9” «0-9»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Assert position at the very end of the string «$»
答案 1 :(得分:1)
没有必要描述所有字符串,结尾就足够了:
SELECT 'Nov. 12th, 60' REGEXP '(^|[^[:digit:]])[[:digit:]]{2}$';
SELECT '99' REGEXP '(^|[^[:digit:]])[[:digit:]]{2}$'
简而言之,最后两位数[[:digit:]]{2}$
前面是非数字字符[^[:digit:]]
,或|
字符串^
的开头。
注意:你可以用较少的posix风格编写它,它不会改变任何东西(只是更短):
SELECT 'Nov. 12th, 60' REGEXP '(^|[^0-9])[0-9]{2}$';