Home >Web Front-end >JS Tutorial >Detailed graphic and text explanation of regular IP matching
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])
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]\.)
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}
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]))
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]))
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]))$
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 = '/^(?:(?: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]))$/'; //正则匹配ip地址 $ip = '254.21.0.198'; preg_match($pattern,$ip,$out); echo '<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);
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
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!