java后台正则表达式验证多种方法--(港澳证件验证)

可以先在浏览器控制台测一下你所写正则表达式是否正确:

/**
     * 是否是港澳证件号
     * 
     * @param str
     * @return
     */
    public  static boolean isIdCard3(String str) {       
    	if(str==null){
    			return false;
		  }else{
		  	String re = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";
		      Pattern p = Pattern.compile(re);
		      Matcher m = p.matcher(str);
		      boolean d = m.matches();
		      System.out.println(d); 
		      return d;
		  }
    } 
    public static boolean isIdCard2(String str) {

    	if (null == str || "".equals(str)) return false;
        String regex = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";
        System.out.println(str.matches(regex));
        return str.matches(regex);
        
    }
    public  static boolean isIdCard(String str) {       
        String regex = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$"; 
        boolean flg = Pattern.matches(regex, str); 
        System.out.println(flg); 
        return flg; 
    }
发布了141 篇原创文章 · 获赞 33 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43560721/article/details/101768750