正則表達式常見應用場景


public class Test01常用效驗 {
    public static void main(String[] args) {
        String[] sArray = null;
        String sRegEx = null;
        System.out.println("------手機號效驗------");
        // 移動號碼段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
        // 聯通號碼段:130、131、132、136、185、186、145
        // 電信號碼段:133、153、180、189
        sArray = new String[] { "13200000001", "15400000002", "13300000003" };
        sRegEx = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        validate(sArray, sRegEx);
        System.out.println("------身份證效驗------");
        sArray = new String[] { "42002719991231000X", "42002719991231A", "420027199912313",
                "42002719991231004" };
        sRegEx = "(^\\d{15}$)|(^\\d{17}(\\d|X|x)$)";
        validate(sArray, sRegEx);
        System.out.println("------郵箱效驗------");
        sArray = new String[] { "andy@163.com", "bob@qq.com", "cat@hotmail.99" };
        sRegEx = "^\\w+@\\w+.[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$";
        validate(sArray, sRegEx);
    }
    static void validate(String[] sArray, String sRegEx) {
        Pattern _pattern = Pattern.compile(sRegEx);
        Matcher matcher = null;
        for (String s : sArray) {
            if (matcher == null) {
                matcher = _pattern.matcher(s);
            } else {
                matcher.reset(s);
            }
            String result = s + (matcher.matches() ? "\t有效" : "\t無效");
            System.out.println(result);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM