Here I will explain a regular expression matching the IP address in detail.
Knowledge about regularity will be mentioned in the detailed explanation.
Before explaining, I will introduce to you the rules for generating IP addresses.
An IP address is composed of a 32-bit binary converted into four decimal strings.
How to convert? The following explanation:
Binary: 1111111111111111111111111111111111111111111
It is divided into four parts: 1111111.11111111.111111111.11111111111111
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.0~255
This is the range of IP addresses.
According to this rules and scope for generating IP, we can use regular expressions to match the IP address, but how to match it? Everyone has their own methods, here I will explain my ideas.
According to the string rules of IP addresses, I divide the expression matching the IP addresses into two parts.
Part 1: Match 3 0~255. (Note the next point)
Part 2: Match the last number 0~255
In other words, first match 0~255. (note the next point) this string, then repeat the match 3 times, and then match the last digit part 0~255. This is my idea of matching the IP address.
First of all, I want to mention that there is no way to do numerical operations, so we cannot filter out the numerical range of IP through numerical operations. Since it is impossible to filter out the numerical range of IP through numerical operations, what other methods should we use to filter out this numerical range? My idea is to discuss in groups, and then combine these divisions to form a digital range of IP.
①. Assuming that the number of IP is a hundred digits, we can draw the following situations based on the number range of IP. Assuming the first number is 1, the range of this number is 1[0-9][0-9]. This should not be difficult to understand, so I will not explain it.
②. Assuming the first number is 2, then according to the range rules of the IP number, there are two situations here. Why? Think about it, the maximum number is 255. When the ten-digit number is 5, the maximum single-digit number can only be 5, right? And when the ten-digit number is 0 to 4, the single-digit number can be any number, right?
So, the two situations here are:
A、2[0-4][0-9]
B、25[0-5]
③. After analyzing the situation of the hundred-digit number, the next thing is the situation of the ten-digit number. If it is a ten-digit number, then the first number in the ten-digit number cannot be zero, right?
So the case of ten-digit numbers can be: [1-9][0-9]
④. What remains is the single digit situation. It is easy for you to draw a conclusion in the single digit situation, which is: [0-9].
After analyzing the four situations, we concluded that the range grouping of IP numbers is:
1[0-9][0-9]
2[0-4][0-9]
25[0-5]
[1-9][0-9]
[0-9]
How to express the above grouping in regular expressions? It's very simple, just use regular or symbol | and grouping symbol (), so the grouping regular expression above 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 of the matching range of the number has been written. So according to my previous idea: Part 1: Match 3 0~255. (Note the next point)
Part 2: Match the last number 0~255
Let’s match the first part of the IP address, and 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 point after each number to match 0~255. (Note the point behind)
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 and match 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, and the next step is to splice the numbers in the second part. The number part has been written very clearly, so I will not 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]))
Having written this, the expression of the regular matching IP has come out, but this is not the final regular expression of the IP. Why? It's very simple. The regular expression will capture and match each packet. The above divides the matching IP into so many packets, and the content of each packet will be captured by the regular. I don't know how many IPs have been captured. Haha, so how can I remove the content of the packet? It's very simple, use this symbol?:
?: The symbol is placed in the () parentheses, which means to capture the grouping but not the content of the regular expression. So, if we put it in each group, will we remove the content of the group? Therefore, we also need to add ?: to each group, and the regularity 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 not matched. We still need to use ^ and $ to limit the beginning and end of the string. Therefore, the last 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 for matching IP addresses. You can learn from it. I hope the readers will ask if there are any bugs, so as not to mislead other readers.
The () brackets of the above regular expression all appear in pairs. If there is any non-pair, please add it yourself. Maybe I missed it.
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]))$/'; //Regularly match IP address$ip = '254.21.0.198'; preg_match($pattern,$ip,$out); echo '<pre>'; 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);