Java Regular Expression verification IP Address

Source: Internet
Author: User
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 ";

String II2 = "127.0.0 .";
String ii3 = "127.0.0.1000 ";
String ii4 = "256.0.0.1 ";
String ii5 = "127.0.0.gg ";
String ii6 = "127. h.0.1 ";
String ii7 = "127.555.0.1 ";
String ii8 = "127.00.0.1 ";
String ii9 = "127.0.04.1 ";
String ii10 = "127.100. #. 1 ";
String ii11 = "127. *. 0.1 ";
String ii12 = "127.0.0.000 ";

String ii13 = "192.168.3.12 ";

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.

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.