2014-05-02 248 views
0

我發現了一個我完全不理解的正則表達式。正方括號後面的正則表達式管道

它看起來像這樣:

([|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(]|) 

我也明白,它試圖匹配對一些喜歡255.255數字,它應該是一個完整的詞。

但是什麼是「([|)」「(] |)」?方括號和最後一箇中的管道看起來也是錯誤的順序。

+0

它的語法有效,但沒有意義。一定是一個錯誤。 – georg

回答

1

krackmoe,有趣的是,沒有([|):這是一種錯覺。

正則表達式引擎不會看到([|)

它看到(這將打開捕獲組1,那麼它看到一個字符類[|)\b(25[0-5]不作一大堆的意義有以下幾個原因。例如,\b與文字字符「b」匹配,字符2和5與範圍0-5重複。

所以你很對不理解它。

我認爲作者想在這裏設置一個字邊界,但就目前而言,這是一個錯字。

僅供參考,這裏是對正則表達式的令牌令牌解釋。 (別擔心,我沒有輸入所有內容,它是由RegexBuddy自動生成的。)

* Match the regex below and capture its match into backreference number 1 `([|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)` 
    * Match this alternative (attempting the next alternative only if this one fails) `[|)\b(25[0-5]` 
     * Match a single character present in the list below `[|)\b(25[0-5]` 
      * A single character from the list 「|)」 `|)` 
      * The character `\b` 
      * A single character from the list 「(25[」 `(25[` 
      * A character in the range between 「0」 and 「5」 `0-5` 
    * Or match this alternative (attempting the next alternative only if this one fails) `2[0-4][0-9]` 
     * Match the character 「2」 literally `2` 
     * Match a single character in the range between 「0」 and 「4」 `[0-4]` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]` 
    * Or match this alternative (the entire group fails if this one fails to match) `[01]?[0-9][0-9]?` 
     * Match a single character from the list 「01」 `[01]?` 
      * Between zero and one times, as many times as possible, giving back as needed (greedy) `?` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]?` 
      * Between zero and one times, as many times as possible, giving back as needed (greedy) `?` 
* Match the character 「.」 literally `\.` 
* Match the regex below and capture its match into backreference number 2 `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)` 
    * Match this alternative (attempting the next alternative only if this one fails) `25[0-5]` 
     * Match the character string 「25」 literally `25` 
     * Match a single character in the range between 「0」 and 「5」 `[0-5]` 
    * Or match this alternative (attempting the next alternative only if this one fails) `2[0-4][0-9]` 
     * Match the character 「2」 literally `2` 
     * Match a single character in the range between 「0」 and 「4」 `[0-4]` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]` 
    * Or match this alternative (the entire group fails if this one fails to match) `[01]?[0-9][0-9]?` 
     * Match a single character from the list 「01」 `[01]?` 
      * Between zero and one times, as many times as possible, giving back as needed (greedy) `?` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]` 
     * Match a single character in the range between 「0」 and 「9」 `[0-9]?` 
      * Between zero and one times, as many times as possible, giving back as needed (greedy) `?` 
* Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) `\b` 
* Match the regex below and capture its match into backreference number 3 `(]|)` 
    * Match this alternative (attempting the next alternative only if this one fails) `]` 
     * Match the character 「]」 literally `]` 
    * Or match this alternative (the entire group fails if this one fails to match) 
1

正則表達式的目的還不清楚。 Debuggex提供了很好的可視化。

Regular expression visualization

Debuggex Demo

約0〜255的部分是清楚的(000,00也接受值)。但是,嘗試匹配|)([]符號的原因不明。

我相信第一個[和最後]出現是因爲錯誤。沒有他們,內部正則表達式看起來是合理的。但(|)\b也看起來不正確,所以我的猜測是我們也可以省略(|)

(|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(|) 

Regular expression visualization

Debuggex Demo