java 正則表達

來源:互聯網
上載者:User

1.驗證身份證

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.驗證ip地址是否同一個段

/** * 驗證兩個ip是否是同一個段 * @param ipStr * @return */public boolean checkIp(String ipStr){Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\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 matcher = pattern.matcher(str[0]);Matcher matcher1 = pattern.matcher(str[1]);if(matcher.matches()==true&&matcher1.matches()==true){//驗證合法int i=str[0].lastIndexOf(".");//得到最後一個.在字串的那個位置String ip0_1=str[0].substring(0,i);//前三段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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.