Regular expression matches an IP expression (recommended)

Source: Internet
Author: User
Tags ip number
Here to give you a detailed explanation of a matching IP address of the regular expression,

Knowledge of the regular aspects will be mentioned in the detailed explanation.

Before I explain, let me introduce you to the rules for generating IP addresses.

The IP address consists of a 32-digit binary converted to four decimal strings.

How to convert? Explained below:

Binary: 11111111111111111111111111111111

Divided into four parts: 11111111.11111111.11111111.11111111

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

Convert to decimal range: 0~255.0~255.0~255.0~255

This is the range of IP addresses.

According to the rule and scope of the generated IP, we can use regular expressions to match the IP address, but how to match it? Everyone has their own way, here I explain my ideas.

Based on the string law of the IP address, I divide the expression of the matching IP address into two parts.

The first part: Match 3 0~255. (Note the next point)

Part II: match the last digit 0~255

That is, first match the 0~255. (Note the next point) of this string, and then repeat the match 3 times, and then match the last digit portion of the 0~255. This is my idea of matching IP addresses.

First of all, I would like to mention that there is no way to do numerical arithmetic, so we cannot filter out the digital range of the IP in the way of numerical operation. Since there is no way to filter out the number range of IP numbers in a digital operation, what other way should we use to filter the range of numbers? My idea is to group discussions, and then merge these groupings together to make up the number range of the IP.

①, assuming that the number of IP is the number of hundred, then according to the IP number range, we can draw the following situations. Assuming that the first number is 1, the range of this number is 1[0-9][0-9]. This should not be difficult to understand, do not explain.

②, assuming that the first number is 2, then according to the IP number range rules, here is divided into two cases, why? You think, the biggest number is 255, when 10 digits is 5 o'clock, the single-digit maximum can only be 5, right? And when the 10-digit number is 0 to 4 o'clock, the single digit can be any number, right?

So, here are the two scenarios:

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

B, 25[0-5]

③, analyzed the hundreds of cases, the next is the 10-digit situation, if it is 10 digits, then the first number of 10-digit number can not be zero, right?

So the 10-digit situation can be: [1-9][0-9]

④, the rest is the single-digit situation, single-digit situation, we should easily come to the conclusion that is: [0-9].

Four cases are analyzed, and we come to the range of IP numbers grouped into:

1[0-9][0-9]

2[0-4][0-9]

25[0-5]

[1-9] [0-9]

[0-9]

How to express the above group with regular expressions? It is easy to use regular or symbolic | and group symbols (), so the above grouping regular expressions are:

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

Write here, the number of matching range regular expression has been written, then according to my previous thinking: The first part: Match 3 0~255. (Note the next point)

Part II: match the last digit 0~255

Let's match the first part of the IP address with the following regular expression:

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

I added a point to the back of each number to match the 0~255. (Note a point later)

So how do you repeat the match three times? Very simple, we just have to take these five groups as a whole, and then repeat the match three times on the line, the regular expression is 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 has been matched, the next is the second part of the stitching on the number, the number of the above has been written very clearly, no longer explain, the following 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]))

Here, the regular match IP expression has come out, but this is not the final matching IP of the regular expression, why? Very simple, the regular expression will be a match for each grouping, the matching IP is divided into so many groupings, and each packet of content will be captured by the regular, it does not know how much IP has been captured, hehe, then how to remove the contents of the group? Very simple, with this symbol?:

?: The symbol is placed inside the () parenthesis, which is the meaning of capturing the grouping, but not capturing the contents of the regular expression. So, let's put it in each of the groupings, don't we get rid of the contents of the group? Therefore, we also have to add to each grouping:, plus the following:

(?:(? : 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]))

Even if we do not match the IP address here, we also use ^ and $ to limit the beginning and end of the string, so the last regular expression to match the IP address is:

^(?:(? : 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])) $

This is my matching IP address the most complete regular expression, you can learn from, what Bug also hope readers, lest mislead other readers.

The above regular expression () parentheses are in pairs appear, if there is no right to appear, please add the reader himself, it may be I miss write.

Here is my test:

<?php$pattern = '/^ (?:(? : 2[0-4][0-9]\.) | (?: 25[0-5]\.) | (?: 1[0-9][0-9]\.) | (?: [1-9][0-9]\.) | (?: [0-9]\.)] {3} (?:(? : 2[0-5][0-5]) | (?: 25[0-5]) | (?: 1[0-9][0-9]) | (?: [1-9][0-9]) | (?: [0-9])) $/';//Regular match IP address $ip = ' 254.21.0.198 ';p reg_match ($pattern, $ip, $out); Echo ' <pre> ';p rint_r ($out); $ip = ' 255.777.0 .198 ';p reg_match ($pattern, $ip, $out);p rint_r ($out) $ip = ' 07.25.8.198 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out $ip = ' 1207.25.8.198 ';p reg_match ($pattern, $ip, $out);p rint_r ($out) $ip = ' qq107.25.8.198 ';p reg_match ($pattern, $ip  , $out);p Rint_r ($out), $ip = ' \.\.\.107.25.8.198 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out); $ip = ' \.\.\. 7.25.8.198 ';p reg_match ($pattern, $ip, $out);p rint_r ($out) $ip = ' 107.25.8.19822VVV ';p reg_match ($pattern, $ip, $out); Print_r ($out); $ip = ' 107.25.r8.1982 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out); $ip = ' 107.225.8.19 ';p reg_match ($ Pattern, $ip, $out);p Rint_r ($out), $ip = ' 225.225.225.225 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out); $ip    = ' 0.0.0.0 ';p reg_match ($pattern, $ip, $out);p rint_r ($out) $ip = ' 00.0.0.0 ';p reg_match ($pattern, $ip, $out);p Rint_r ($o UT); $ip = ' 0.202.1.0 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out); $ip = ' 0.202.1.226 ';p reg_match ($pattern, $ip, $out );p Rint_r ($out), $ip = ' 249.202.1.0 ';p reg_match ($pattern, $ip, $out);p Rint_r ($out); $s = "; for ($i =0; $i <32; $i + +) {$s . = ' 1 ';} echo $s; Echo strlen ($s);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.