2016-05-31 80 views
0

我在數據庫表中有一些URL,而不http://MySQL的REGEXP與字邊界後斜線

 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

嘗試'「[[:<:]] 10.1.127.4 /」' –

+0

嗯,我挺想的吧:) – Anse

+0

這是沒有意義的字邊界,T在'/'和字符串結尾之間沒有字邊界。 –

回答

0

發現,[[:>:]]期望單詞字符的,反之亦然爲[[:<:]]

簡單的測試驗證:

SELECT 'bla,,123' REGEXP '[[:<:]]bla,[[:>:]]' -- no match 
SELECT 'bla,,123' REGEXP '[[:<:]]bla[[:>:]]' -- match 
SELECT 'bla,,123' REGEXP '[[:<:]]bla,,123[[:>:]]' -- match 

我認爲這種方式的文件是有道理的,我misunderstoo d相當於幾年:

字界線。它們匹配的開始和單詞的結束, [...]

所以,一個字邊界預計

  1. 的一面
  2. 非單詞字符和單詞字符在另一邊
1

按照文檔:http://dev.mysql.com/doc/refman/5.7/en/regexp.html

[[:<:]],[[:>:]]

這些標記代表單詞邊界。它們分別與詞尾和詞尾相匹配。一個單詞是一個單詞字符序列,其前面或後面沒有單詞字符。字 字符是alnum類中的字母數字字符或 下劃線(_)。

/不是一個alnum成員,因此它不是一個字邊界。

+0

*'/'不是一個alnum成員,因此它不是一個字邊界。* - 不,在**'/'和字符串結尾之間沒有字邊界。字邊界是一個零寬度斷言。 –

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) «[[:>:]]»