How to match the regular expression is a string IP address

We are here to explain in detail a matching IP address regular expression.

Before explaining, I give you about, ip address generation rules.

IP address, is a 32-bit binary numbers into decimal four strings.

How transformation? The following explanation:

      Binary: 11111111111111111111111111111111

      It is divided into four parts: 11111111.11111111.11111111.11111111

      Conversion: 7 + 2 ^ 2 ^ 2 ^ 6 + 5 + 4 + 2 ^ 2 ^ 2 ^ 2 + 3 + 2 + 2 ^ 0 ^ 1 = 255

      To decimal range: 0 ~ 255 ~ 255 ~ 255.0 ~ 255.0

      This is the range of IP addresses.

According to this generation IP rules and scope, we can use regular expressions to match the IP address, but how to match it? Everyone has his own way, and here I explain my ideas.

According to the law of a string of IP addresses, IP addresses match I put expression into two parts to consider.

      First part: matching 3 0 to 255 (note that a point later).

      Second part: matching the last digit 0 to 255

      That is, the first match 0 to 255. (Note that a point behind) this string, then repeated three times match, then match the last portion of the digital 0 to 255. This is my idea of ​​matching IP addresses.

       First of all, I would like to mention, is the number-crunching is no way to do, so we can not use digital operation mode selected range of numbers of IP. Since it can not filter out a range of IP numbers by way of number crunching, then we should be any other way to filter this range of numbers to use it? My idea is to group discussions, then put them together to form the IP packet combining a range of numbers.

       ①, assuming that IP numbers are hundreds digit, then according to a range of IP numbers, we can draw the following situations. Assume that the first digit is 1, then the scope of this figure is 1 to [0-9] [0-9]. This should not be difficult to understand, not explained.

      ②, assume that the first digit is 2, then according to the scope of the rules of IP numbers, but also divided into two cases here, and why? Just think, the maximum number is 255, when tens digit is 5, only a maximum of 5 digits is not it? And when the ten digits is 0-4, the single digits may be any number, right?

So, here are two cases:

           A、2[0-4][0-9]

           B、25[0-5]

       ③, analysis of the situation over the hundreds digit, the next is the case of ten-digit, and if it is ten digits, the previous ten-digit number can not be zero first, right?

So tens of cases may be: [1-9] [0-9]

       ④, the rest is the single digits situation, the situation in the single digits, we should be very easy to draw the conclusion that: [0-9].

Four cases analysis down, we come to the range of IP numbers grouped as follows:

        1[0-9][0-9]

        2[0-4][0-9]

        25[0-5]

        [1-9][0-9]

        [0-9]

 How the above expression represents a group with a positive out of it? Very simple, with regular or symbols | and grouping symbol () on it, so the above grouping the regular expression is:

(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])

 

Write here, the range of numbers match the regular expression has been written, then in accordance with the foregoing ideas I: Part I: matching 3 0 to 255 (note that a point later).

         Second part: matching the last digit 0 to 255

 We first part of the IP address to match the regular expression as follows: 

(1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)

I added after each number is matched with a point 0 to 255. (Note that a point later)

 So how is repeated three times to match it? Very simple, as long as we put the five groups as a whole, and then repeat three times on the line matching, regular expression as follows: 

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

The first part of the match has been out, the next step is digital stitching on the second part of the digital section above has been written very clearly, no longer explain, Here is the complete regular expression:

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

 

Published 69 original articles · won praise 52 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_38983511/article/details/104720211