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 regular expressions verify the function of IPV4 addresses

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

Share

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

This article is about how regular expressions verify the functionality of IPV4 addresses. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows:

The IPV4 address consists of four groups of digits, each of which is separated by. Separated, the range of values for each group of numbers is 0-255.

IPV4 must meet the following four rules:

1. Any 1-digit or 2-digit number, that is, 0-99

2. Any three-digit number that begins with 1, that is, 100-199

3. Any number that starts with 2 and the second digit is a three-digit number between 0 and 4, that is, 200-249

4. Any three-digit number that begins with 25 and whose third digit is between 0 and 5, that is, 250-255.

After listing all the rules in this way, the idea of constructing a regular expression is clear.

The first rule that satisfies the first rule is:\ d {1pr 2}

The first rule that satisfies the second rule is: 1\ d {2}

The first rule that satisfies the third rule is: 2 [0-4]\ d

The first rule that satisfies the fourth rule is: 25 [0-5]

Put them together and you get a regular expression that matches 0-255 numbers:

(\ d {1pr 2}) | (1\ d {2}) | (2 [0-4]\ d) | (25 [0-5])

IPV4 consists of four sets of such numbers, with. Separated, or by three sets of numbers and characters. And a set of numbers, so the regular expression that matches IPV4 is as follows:

(\ d {1pr 2})) | (1\ d {2}) | (2 [0-4]\ d) | (25 [0-5]))\.) {3} ((\ d {1pr 2}) | (2 [0-4]\ d) | (25 [0-5])

The Java test code is as follows:

Public static void matchAndPrint (String regex, String sourceText) {Pattern pattern = Pattern.compile (regex); Matcher matcher = pattern.matcher (sourceText); while (matcher.find ()) {System.out.println (matcher.group ()) } public static void main (String [] args) {String regex = "^ (\\ d {1pr 2})) | (1\\ d {2}) | (2 [0-4]\ d) | (25 [0-5]))\\.) {3} ((\\ d {1p2}) | (1\\ d {2}) | (2 [0-4]\ d) | (25 [0-5])) $"; matchAndPrint (regex, "23.135.2.255") MatchAndPrint (regex, "255.255.0.256"); matchAndPrint (regex, "0.0.0.0");}

The output is as follows:

23.135.2.255

0.0.0.0

One drawback of this rule is that if boundary matching is not used, the second test IP 255.255.0.256 will also be matched, and the matching result will be 255.255.0.25. You can add constraints, either bounded or non-numeric, and use pre-and post-search (lookaround), which is described later. That is:

(?

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