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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.
