正则表达式常见应用场景

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[] { "[email protected]", "[email protected]", "[email protected]" };
        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);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/eyasfly/p/12380398.html