Java Regular Expression

Source: Internet
Author: User

1. Verify your ID card

public static boolean checkIdentityCard(String identity_card){String pattern = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4})((((19|20)(([02468][048])|([13579][26]))0229))|((20[0-9][0-9])|(19[0-9][0-9]))((((0[1-9])|(1[0-2]))((0[1-9])|(1\\d)|(2[0-8])))|((((0[1,3-9])|(1[0-2]))(29|30))|(((0[13578])|(1[02]))31))))((\\d{3}(x|X))|(\\d{4}))";  Pattern p = Pattern.compile(pattern);  Matcher m = p.matcher(identity_card);  return m.matches();}

2. Verify that the IP address is in the same segment

/*** Verify whether the two IP addresses are in the same segment * @ Param ipstr * @ return */Public Boolean checkip (string ipstr) {pattern = pattern. compile ("\ 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 "); // string ipstr =" 192.168.1.12/192.168.1.12 "; string [] STR = ipstr. split ("/"); If (Str. length = 2) {matcher = pattern. matcher (STR [0]); matcher matcher1 = pattern. matcher (STR [1]); If (matcher. matches () = true & matcher1.matches () = true) {// verify valid int I = STR [0]. lastindexof (". "); // get the last one. string ip0_1 = STR [0] at the position of the string. substring (0, I); // the first three paragraphs int ip0_4 = integer. valueof (STR [0]. substring (I + 1, STR [0]. length (); // system. out. println (ip0_1 + "====" + ip0_4); Int J = STR [1]. lastindexof (". "); string ip1_1 = STR [1]. substring (0, I); int ip1_4 = integer. valueof (STR [1]. substring (I + 1, STR [1]. length (); // system. out. println (ip1_1 + "======" + ip1_4); If (ip0_1.equals (ip1_1) & ip0_4 <= ip1_4) {return true ;}} return false ;}

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.