Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an expression that matches IP in regular expressions

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to implement an expression that matches IP in a regular expression. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Before I explain, let me introduce to you the rules for the generation of ip addresses.

An IP address, which consists of a 32-bit numeric binary converted to four decimal strings.

How to convert? The following is the explanation:

Binary: 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

It is divided into four parts: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

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

To decimal range: 0" 255.0" 255.0" 255.0" 255

This is the range of IP addresses.

According to this rule and scope for generating IP, we can use regular expressions to match IP addresses, but how do we match them? Each person has his own way, here I will explain my train of thought.

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

The first part: match 3 0255s. (pay attention to the following point)

Part two: match the last number 0255,

That is, the string is matched first (note the following dot), then repeated three times, and then the last part of the number 00255is matched. This is how I match the IP address.

First of all, I would like to mention that there is no way for regular to do numerical operations, so we cannot filter out the numerical range of IP by means of numerical operations. Since it is impossible to filter out the numeric range of IP by numeric operation, what other way should we use to filter this numeric range? My idea is to discuss it in groups, and then combine these groups to form a range of numbers for IP.

①, assuming that the number of IP is a hundred digits, then according to the number range of IP, we can draw the following situations. Assuming that the first number is 1, the range of this number is 1 [0-9] [0-9]. This should not be difficult to understand, so do not explain.

②, assuming that the first number is 2, then according to the range rules of IP numbers, there are two cases here. Why? If you think about it, the maximum number is 255. when the ten digits are 5, the maximum number of single digits can only be 5, right? And when the ten digits are 0 to 4, the single digits 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 100 digits, then there is the case of ten digits. If it is ten digits, then the first number of ten digits can't be zero, right?

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

④, what's left is the single-digit situation, and in the single-digit case, it should be easy to draw a conclusion, that is, [0-9].

Based on the analysis of the four situations, we find 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? Quite simply, regular or symbol | and grouping symbol () are fine, 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]))

At this point, the regular expression for the matching range of numbers has been written, so according to my previous train of thought: the first part: match 3 0255. (pay attention to the following point)

Part two: match the last number 0255,

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 at the end of each number to match the zero 255. (pay attention to the following point)

So how do you repeat the match three times? Quite simply, all we have to do is to take the five groups as a whole and repeat the 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 concatenate the numbers in the second part. The number part is clearly written on it, so it will not be explained any more. Here is the complete regular expression:

((1 [0-9] [0-9]\.) | (2 [0-4] [0-9]\.) | (25 [0-5]\.) | ([1-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, there is a regular expression that matches IP, but this is not the final regular expression that matches IP. Why? Very simply, the regular expression will capture and match each packet. The matching IP is divided into so many packets, and the content of each packet will be captured by the rule. I don't know how much IP has been captured above, hehe, so how to remove the content of the group? It's very simple. Use this symbol?

The symbol is placed in () parentheses to capture the grouping, but not the content of the regular expression. So, if we put it in each group, won't we get rid of the content of the group? So, we have to add?: to each group, and the rules are as follows:

(?: 1 [0-9] [0-9]\.) | (?: 2 [0-4] [0-9]\.) | (?: 25 [0-5]\.) | (: [1-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 we haven't matched the IP address here, we still have to limit the beginning and end of the string with ^ and $, so the final regular expression that matches the IP address is:

^ (?): 1 [0-9] [0-9]\.) | (?: 2 [0-4] [0-9]\.) | (?: 25 [0-5]\.) | (?: [1-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 me to match the IP address. You can use it for reference. If there is any bug, I hope the reader will put forward it, so as not to mislead other readers.

The () parentheses of the above regular expression all appear in pairs. If there are no pairs, please add it yourself. Maybe I missed it.

Here are my tests:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report