我在数据库表中有一些没有http://
的网址:
url
row #1: 10.1.127.4/
row #2: 10.1.127.4/something
现在,以下过滤器为我提供第2行 - 罚款:
SELECT * FROM mytable WHERE url REGEXP '[[:<:]]10.1.127.4/something[[:>:]]'
但是下面的过滤器不会给我第1行,但不应该这样吗?
SELECT * FROM mytable WHERE url REGEXP '[[:<:]]10.1.127.4/[[:>:]]'
我应该注意,通过反斜杠转义正斜杠也不会返回想要的行#1:
SELECT * FROM mytable WHERE url REGEXP '[[:<:]]10.1.127.4\/[[:>:]]'
答案 0 :(得分:1)
根据文档:http://dev.mysql.com/doc/refman/5.7/en/regexp.html
[[:&lt;:]],[[:&gt;:]]
这些标记代表字边界。他们匹配开头和 词尾,分别。一个单词是一系列单词字符 不在单词字符之前或之后的单词字符。一个字 character是alnum类中的字母数字字符或 下划线(_)。
/
不是alnum会员,因此它不是单词边界。
答案 1 :(得分:0)
SELECT * FROM mytable WHERE mycolumn REGEXP "[[:<:]][0-9]{1,3}\\.([0-9]{1,3}.?){3}((\\/)?[^ ]*)?[[:>:]]";
[[:<:]][0-9]{1,3}\.([0-9]{1,3}.?){3}((\/)?[^ ]*)?[[:>:]]
Assert position at the beginning of a word (position followed by but not preceded by an ASCII letter, digit, or underscore) «[[:<:]]»
Match a single character in the range between “0” and “9” «[0-9]{1,3}»
Between one and 3 times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «{1,3}»
Match the character “.” literally «\.»
Match the regex below and capture its match into backreference number 1 «([0-9]{1,3}.?){3}»
Exactly 3 times «{3}»
You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «{3}»
Match a single character in the range between “0” and “9” «[0-9]{1,3}»
Between one and 3 times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «{1,3}»
Match any single character that is NOT a line break character (line feed) «.?»
Between zero and one times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «?»
Match the regex below and capture its match into backreference number 2 «((\/)?[^ ]*)?»
Between zero and one times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «?»
Match the regex below and capture its match into backreference number 3 «(\/)?»
Between zero and one times, as few or as many times as needed to find the longest match in combination with the other quantifiers or alternatives «?»
Match the character “/” literally «\/»
Match any single character that is NOT present in the list below and that is NOT a line break character (line feed) «[^ ]*»
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 «*»
The literal character “ ” « »
Assert position at the end of a word (position preceded by but not followed by an ASCII letter, digit, or underscore) «[[:>:]]»
答案 2 :(得分:0)
发现[[:&gt;:]]需要左的单词字符,反之亦然[[:&lt;:]]
简单测试验证:
SELECT 'bla,,123' REGEXP '[[:<:]]bla,[[:>:]]' -- no match
SELECT 'bla,,123' REGEXP '[[:<:]]bla[[:>:]]' -- match
SELECT 'bla,,123' REGEXP '[[:<:]]bla,,123[[:>:]]' -- match
我认为文档的这种方式很有意义,而且我误解了很多年:
[...]字边界。它们匹配单词的开头和结尾, [...]
因此,字边界需要