2014-01-22 56 views
-1

任何人都可以请解释我这个正则表达式的含义吗?正则表达式的解释

/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/ 

回答

12

我认为这正则表达式将验证IPv4地址

请使用此链接以获得正则表达式

Regex Explanation

 

    NODE      EXPLANATION 
    -------------------------------------------------------------------------------- 
    ^      the beginning of the string 
    -------------------------------------------------------------------------------- 
     (      group and capture to \1: 
    -------------------------------------------------------------------------------- 
     [1-9]     any character of: '1' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
     [1-9]     any character of: '1' to '9' 
    -------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
     1      '1' 
    -------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
     2      '2' 
    -------------------------------------------------------------------------------- 
     [0-4]     any character of: '0' to '4' 
    -------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
     25      '25' 
    -------------------------------------------------------------------------------- 
     [0-5]     any character of: '0' to '5' 
    -------------------------------------------------------------------------------- 
    )      end of \1 
    -------------------------------------------------------------------------------- 
     (      group and capture to \2 (3 times): 
    -------------------------------------------------------------------------------- 
     .      any character except \n 
    -------------------------------------------------------------------------------- 
     (      group and capture to \3: 
    -------------------------------------------------------------------------------- 
      [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
      [1-9]     any character of: '1' to '9' 
    -------------------------------------------------------------------------------- 
      [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
      1      '1' 
    -------------------------------------------------------------------------------- 
      [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
      [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
      2      '2' 
    -------------------------------------------------------------------------------- 
      [0-4]     any character of: '0' to '4' 
    -------------------------------------------------------------------------------- 
      [0-9]     any character of: '0' to '9' 
    -------------------------------------------------------------------------------- 
     |      OR 
    -------------------------------------------------------------------------------- 
      25      '25' 
    -------------------------------------------------------------------------------- 
      [0-5]     any character of: '0' to '5' 
    -------------------------------------------------------------------------------- 
     )      end of \3 
    -------------------------------------------------------------------------------- 
    ){3}      end of \2 (NOTE: because you are using a 
           quantifier on this capture, only the LAST 
           repetition of the captured pattern will be 
           stored in \2) 
    -------------------------------------------------------------------------------- 
     $      before an optional \n, and the end of the 
           string 

+0

它与IP地址匹配吗? – Geezer68

+0

@csmckelvey是的,我认为它验证IPv4地址 –

+0

我不知道,但你只是调试所有这些垃圾PHP脚本我解码微风....谢谢你!评论中的链接的总赞成数。 –

5

的explation虽然上述回答按原子解释正则表达式原子,我认为你是答案寻找的是“它匹配IPv4地址”。

要机智:

# Match the beginning of a string 
/^ 
# Match a number from 1-255 
([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) 
# Same as above with a . in front of it 
(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])) 
# Match the above three times. 
{3} 
# Match end of the string 
$/ 
4

它看起来像接受来自1.0.0.0255.255.255.255

更好的解释的表达式:

enter image description here

Meaning

enter image description here

+2

我对你如何生成图表更感兴趣? – BMW

+1

http://www.regexper.com/ –