There are many expressions for verifying IP addresses using regular expressions on the Internet. You can search a large number of expressions and write them on your own. However, it is very troublesome and troublesome. It is inevitable that someone else will write a bug. I found a few tests. I don't know any bugs or even the correct IP addresses. Many of them are still vowed, as if I had tested them myself. Today, I found a regular expression for comparison, but there is also a small bug (no error is reported when the * number is used). Package des; import java. util. RegEx. matcher;
Import java. util. RegEx. pattern; public class IP {/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Ip p = new IP ();
String II = "127.0.0.1 ";
P. ismatches (II );
P. ismatches (II2 );
P. ismatches (ii3 );
P. ismatches (ii4 );
P. ismatches (ii5 );
P. ismatches (ii6 );
P. ismatches (ii7 );
P. ismatches (ii8 );
P. ismatches (ii9 );
P. ismatches (ii10 );
P. ismatches (ii11 );
P. ismatches (ii12 );
P. ismatches (ii13 );
}
Public Boolean ismatches (string IP ){
Boolean flag = false;
Try {
// String RegEx = "[0-9] + [.] [0-9] + [.] [0-9] + [.] [0-9] + ";
String RegEx = "^ (\ d | [1-9] \ d | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5] | [*]) \\.) {3} (\ d | [1-9] \ d | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5] | [*]) $ ";
// The correct one is // string RegEx = "^ (\ d | [1-9] \ d | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \\.) {3} (\ d | [1-9] \ d | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5] | [*]) $ ";
// String RegEx = "\ B ((?! \ D) \ D + | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \\. ((?! \ D) \ D + | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \\. ((?! \ D) \ D + | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \\. ((?! \ D) \ D + | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \ B ";
Pattern P = pattern. Compile (RegEx );
Matcher M = P. matcher (IP );
If (M. Find ()){
System. Out. println ("successss ");
Return true;
} Else {
System. Out. println ("falsss ");
}
} Catch (exception e ){
E. printstacktrace ();
}
Return flag;
}
} Output successss
Falsss
Falsss
Falsss
Falsss
Falsss
Falsss
Falsss
Falsss
Falsss
Successss
Falsss
Successss
The last 3rd are incorrect, but the output is successful.
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.