Regular expressions match IP expressions (recommended)

Source: Internet
Author: User
Tags ip number
A regular expression, also known as a rule expression. Next, we will introduce the regular expression matching IP address through this article. it is very good and has reference value. For more information, see the regular expression matching IP address,

The knowledge about regular expressions is described in detail.

Before explaining this, I would like to introduce the rules for generating IP addresses.

The IP address is composed of 32-bit binary numbers into four decimal strings.

How to convert? The following explains:

Binary: 11111111111111111111111111111111

Divided into four parts: 11111111.11111111.1111111111

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

Decimal range: 0 ~ 255.0 ~ 255.0 ~ 255.0 ~ 255

This is the IP address range.

Based on the rules and ranges of the generated IP addresses, we can use regular expressions to match the IP addresses, but how can we match them? Each person has his/her own method. here I will explain my ideas.

According to the character string of the IP address, I divide the expression that matches the IP address into two parts.

Part 1: match 3 0 ~ 255. (pay attention to the next point)

Part 2: match the final number 0 ~ 255

That is to say, first match 0 ~ 255. (pay attention to the next point) the string, repeat it three times, and then match the last digit part 0 ~ 255. This is my idea of matching IP addresses.

First of all, I would like to mention that there is no way to perform numeric operations for regular expressions. Therefore, we cannot use numeric operations to filter the IP address range. Since it is impossible to filter the numeric range of an IP address by number, what other method should we use to filter the numeric range? My idea is to discuss these groups in groups, and then combine these groups to form a numerical range of IP addresses.

① If the number of IP addresses is a hundred bits, then the following conditions can be obtained based on the number range of IP addresses. Assume that the first digit is 1, and the value range is 1 [0-9] [0-9]. This should not be difficult to understand.

2. if the first number is 2, the IP address number range rules are further divided into two situations. why? You think, the maximum number is 255. when the ten-digit number is 5, the maximum number of single-digit numbers is 5, right? When the ten digits are 0 to 4, the single digit can be any number, right?

Therefore, the two cases here are:

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

B, 25 [0-5]

③ After analyzing the situation of hundreds of digits, there will be ten digits. if there are ten digits, then the first digit of the ten digits cannot be zero, right?

Therefore, the number of digits can be [1-9] [0-9].

④ The rest is the single-digit situation. in the single-digit situation, you can easily conclude that: [0-9].

After analyzing the four conditions, we can obtain the IP number range grouping:

1 [0-9] [0-9]

2 [0-4] [0-9]

25 [0-5]

[1-9] [0-9]

[0-9]

How can we use a regular expression to represent the group above? It is very easy to use the regular expression or symbol | and the grouping symbol (), so the group regular expression above is:

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

Here, the regular expression for the matching range of numbers has been written, so according to my previous thought: Part 1: match 3 0 ~ 255. (pay attention to the next point)

Part 2: match the final 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 add a point to the end of each number to match 0 ~ 255. (pay attention to the next point)

So how can we repeat 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 second part of the number. The number is clearly written above and will not be explained. Below is a 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 expression of the regular expression matching the IP address has come out. However, this is not the final regular expression matching the IP address. why? The regular expression captures and matches each group. The matching IP address is divided into so many groups, and the content of each group is captured by the regular expression, I don't know how many IP addresses have been captured. how can I remove the group content? Is it easy to use this symbol? :

? The symbol is placed in the parentheses () to capture the group, but not the content of the regular expression. So we put it in each group, so we will not remove the group content? So we need to add? :, Followed by the regular expression 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 if the IP address is not matched here, we still need to use ^ and $ to limit the start and end of the string. Therefore, the regular expression of the last matched 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 refer to it for any bugs that may be raised by readers to avoid misleading other readers.

The parentheses of the above regular expression appear in pairs. if not, please add them by yourself. maybe I have missed them.

The following is my test:

 '; 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 = '2017. 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 = '2017. 25.8.19822vvv '; preg_match ($ pattern, $ ip, $ out); print_r ($ out); $ ip = '2017. 25. r8.1982 '; preg_match ($ pattern, $ ip, $ out); print_r ($ out); $ ip = '2017. 225.8.19 '; preg_match ($ pattern, $ ip, $ out); print_r ($ out); $ ip = '2017. 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 = '2017. 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 );

For more articles about regular expressions matching IP expressions (recommended), please follow the PHP Chinese website!

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.