正则表达式的解释

时间:2014-01-22 05:01:45

标签: regex

有人可以解释一下这个正则表达式意味着什么吗?

/^([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}$/

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:5)

虽然上面的答案通过原子解释了正则表达式原子,但我认为您正在寻找的答案是“它与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
$/

答案 2 :(得分:4)

它看起来像是接受1.0.0.0255.255.255.255

之类的值的表达式

更好的解释:

enter image description here

Meaning

enter image description here