可以先在浏览器控制台测一下你所写正则表达式是否正确:
/**
* 是否是港澳证件号
*
* @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;
}