正则表达式
基础判断代码:
1 public class Test { 2 public static void main(String[] args) { 3 String string = "20721938472017411x"; 4 Pattern pattern = Pattern 5 .compile("(^\\d{18})|(^\\d{15})|(^\\d{17}(X|x))"); 6 Matcher matcher = pattern.matcher(string); 7 System.out.println("身份证:" + string + " 长度:" + string.length() + "\n" 8 + matcher.matches()); 9 } 10 11 }
输出:
身份证:20721938472017411x 长度:18
true