2012-02-02 17 views
0

解碼的正則表達式誰能解碼什麼這個正則表達式是指在Perl:在Perl

while (/([0-9a-zA-Z\-]+(?:'[a-zA-Z0-9\-]+)*)/g)

回答

3

這裏是正則表達式的細分:

(     # start a capturing group (1) 
    [0-9a-zA-Z-]+  # one or more digits or letters or hyphens 
    (?:    # start a non-capturing group 
     '    # a literal single quote character 
     [a-zA-Z0-9-]+ # one or more digits or letters or hyphens 
    )*     # repeat non-capturing group zero or more times 
)      # end of capturing group 1 

的正則表達式是形式/.../g並在一個while循環中,這意味着while中的代碼將針對regex的每個非重疊匹配運行。

+2

在這兩種情況下,字符類是「一個或多個數字或字母和連字符」。 – 2012-02-02 18:15:28

+0

@davorg - 謝謝,我沒有注意到'-'後面。 – 2012-02-02 18:18:39

2

F.J的回答是一個完美的細分。但是......他遺漏了一個重要的部分,最後是/ g。它告訴解析器繼續上次離開的位置。所以while循環將繼續循環遍歷字符串,直到它獲得沒有其他點匹配的點。

3

有該工具:YAPE::Regex::Explain

The regular expression: 

(?-imsx:([0-9a-zA-Z\-]+(?:'[a-zA-Z0-9\-]+)*)) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [0-9a-zA-Z\-]+   any character of: '0' to '9', 'a' to 
          'z', 'A' to 'Z', '\-' (1 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     '      '\'' 
---------------------------------------------------------------------- 
     [a-zA-Z0-9\-]+   any character of: 'a' to 'z', 'A' to 
           'Z', '0' to '9', '\-' (1 or more times 
           (matching the most amount possible)) 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
)      end of grouping 
----------------------------------------------------------------------