Home > Web Front-end > JS Tutorial > body text

Detailed graphic and text explanation of regular IP matching

php中世界最好的语言
Release: 2018-03-30 11:36:56
Original
1617 people have browsed it

This time I will bring you a detailed picture and text explanation of regular matching IP. What are the precautions for regular matching IP? The following is a practical case, let’s take a look.

Here I will give you a detailed explanation of a regular expression that matches an IP address.

Knowledge about regular expressions will be mentioned in the detailed explanation.

Before explaining, let me first introduce to you the rules for generating IP addresses.

The IP address is composed of a 32-digit binary number converted into four decimal strings.

How to convert? Explained below:

Binary: 111111111111111111111111111111

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 the IP address.

Based on the rules and range of IP generation, we can use regular expressions to match the IP address, but how to match? Everyone has their own method, here I will explain my approach.

Based on the string rules of the IP address, I divided the expression matching the IP address into two parts to consider.

The first part: Match 3 0~255. (note the dot at the end)

The second part: Match the last number 0~255

In other words, First match the string 0~255. (note the dot at the end), then repeat the match 3 times, and then match the last number part 0~255. This is my idea of ​​matching IP addresses.

First of all, I want to mention that there is no way to do numerical operations with regular expressions. Therefore, we cannot use numerical operations to filter out the numerical range of IP. Since there is no way to filter out the numerical range of IP using number operations, what other methods should we use to filter this numerical range? My idea is to groupdiscuss, and then merge these groups to form the IP range.

① Assuming that the IP number is in the hundreds digit, then based on the IP number range, we can draw the following situations. Assuming that the first number is 1, then the range of this number is 1[0-9][0-9]. This should not be difficult to understand, so I won’t explain it.

②. Assuming that the first number is 2, then according to the range rules of IP numbers, there are two situations here. Why? Think about it, the largest number is 255. When the tens digit is 5, the single digit can only be 5 at most, right? And when the tens digit is 0 to 4, the ones digit can be any number, right?

So, the two situations here are:

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

B, 25[0-5]

③After analyzing the hundreds digit situation, the next step is the tens digit situation. If it is a tens digit, then the first number in front of the tens digit cannot be zero, right?

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

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

After analyzing the four situations, we came to the conclusion that the range of IP numbers is grouped as:

   1[0-9][0-9]

   2[0- 4][0-9]

25[0-5]

## How to express the above grouping using regular expressions? It's very simple, just use the regular or symbol | and the grouping symbol (), so the above grouping regular expression is:

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

Having written this, the regular expression for the matching range of numbers has been written, so according to my previous ideas: Part 1: Match 3 0~255. (Pay attention to the following dot)

Part 2: Match the last number 0~255

Let’s match the first part of the IP address. 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]\.)
Copy after login

I added a dot after each number. Matches 0~255. (Pay attention to the following point)

So how do you repeat the match three times? It's very simple. We just need to treat these five groups as a whole and repeat the matching three times. 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}
Copy after login

The first part has been matched. The next step is to splice the numbers in the second part. , the numerical part has been clearly written above, so I won’t explain it anymore. 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]))
Copy after login

At this point, the regular expression for matching IP has come out, but this does not mean Not the final regular expression to match IP, why? It's very simple. The regular expression will capture and match each group. The matching IP is divided into so many groups, and the content of each group will be captured by the regular expression. I don't know how many IPs have been captured above, haha, So how to remove the grouped content? It's very simple. Use this symbol?:

. The ?: symbol is placed inside () parentheses. It captures the group but does not capture the content of the regular expression. So, if we put it in each group, won't we remove the content of the group? Therefore, we also need to add ?: to each group, and the regular expression after adding is as follows:

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

Even here, the IP address is still not matched, and we still need to use ^ and $ to limit the string The beginning and end of , so the final regular expression matching 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]))$
Copy after login

This is the most complete regular expression I used to match the IP address. You can learn from it. If there are any bugs, I hope readers will raise them. , so as not to mislead other readers.

The () brackets in the above regular expressions all appear in pairs. If there are any unpaired ones, please add them yourself. Maybe I missed it.

The following is my test:

<?php
$pattern = &#39;/^(?:(?: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]))$/&#39;;
//正则匹配ip地址
$ip     = &#39;254.21.0.198&#39;;
preg_match($pattern,$ip,$out);
echo &#39;<pre class="brush:php;toolbar:false">';
print_r($out);
$ip     = '255.777.0.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '07.25.8.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '1207.25.8.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = 'qq107.25.8.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '\.\.\.107.25.8.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '\.\.\.  7.25.8.198';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '107.25.8.19822vvv';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '107.25.r8.1982';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '107.225.8.19';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '225.225.225.225';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '0.0.0.0';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '00.0.0.0';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '0.202.1.0';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '0.202.1.226';
preg_match($pattern,$ip,$out);
print_r($out);
$ip     = '249.202.1.0';
preg_match($pattern,$ip,$out);
print_r($out);
$s='';
for($i=0;$i<32;$i++){
  $s .= '1';
}
echo $s;
echo strlen($s);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Regular implementation of numbers in js with spaces separated every four digits

In PHP Detailed explanation of efficient greedy, non-greedy and backtracking using regular expressions (with code)

The above is the detailed content of Detailed graphic and text explanation of regular IP matching. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!