Analysis of IPV4 address function instances using regular expressions

Source: Internet
Author: User
This article mainly introduces the function of regular expression to verify IPV4 addresses, and analyzes the principles and implementation skills of IPV4 address verification in the form of examples, for more information about how to use regular expressions to verify IPV4 addresses, see the following example. We will share this with you for your reference. The details are as follows:

An IPV4 address consists of four groups of numbers, which are separated by.. the value range of each group of numbers is 0-255.

IPV4 must meet the following four rules:

1. any one-or two-digit number, that is, 0-99;
2. any three-digit number starting with 1, that is, 100-199;
3. any three-digit number starting with 2 and 2nd digits between 0 and 4, that is, 200-249;
4. any three-digit number starting with 25 with a 3rd-digit number between 0 and 5, that is, 250-255.

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

First, the regular expression that satisfies the first rule is \ d {1, 2}
First, the regular expression that satisfies the second rule is: 1 \ d {2}
First, the regular expression that satisfies the third rule is: 2 [0-4] \ d
First, the regular expression that satisfies the fourth rule is: 25 [0-5].

Combine them to get a regular expression that matches numbers 0:

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

IPV4 consists of four groups of numbers, separated by. in the middle, or composed of three groups of numbers and characters. and a group of numbers. Therefore, the regular expression matching IPV4 is as follows:

(\ D {1, 2}) | (1 \ d {2}) | (2 [0-4] \ d) | (25 [0-5]) \.) {3} (\ d {1, 2}) | (1 \ d {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{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(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 result is as follows:

23.135.2.255
0.0.0.0

This regular expression has a defect, that is, if boundary matching is not used, IP segment 255.255.0.256 in the second Test will also be matched. the matched result is 255.255.255.0.25. You can add a restriction condition, which can be either a boundary or a non-number, and use lookaround. That is:

(? <= (\ B | \ D) (\ d {1, 2}) | (1 \ d {2 }) | (2 [0-4] \ d) | (25 [0-5]) \.) {3} (\ d {1, 2}) | (1 \ d {2}) | (2 [0-4] \ d) | (25 [0-5]) (? = (\ B | \ D ))

String regex = "(? <= (\ B | \ D) (\ d {1}) | (1 \ d {2 }) | (2 [0-4] \ d) | (25 [0-5]) \.) {3} (\ d {1, 2}) | (1 \ d {2}) | (2 [0-4] \ d) | (25 [0-5]) (? = (\ B | \ D ))";

This solves the problem.

I hope this article will help you learn regular expressions.

For more articles about the instance analysis of the IPV4 address function verified by regular expressions, refer to PHP Chinese network!

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.