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!