定义正则表达式
private static String regID16 = "\\d{16}";
private static String regID19 = "\\d{19}";
匹配字符串
Pattern ptr1 = Pattern.compile(regID16);
Pattern ptr2 = Pattern.compile(regID19);
Matcher m1 = ptr1.matcher(str);
Matcher m2 = ptr2.matcher(str);
boolean b = m1.matches() || m2.matches();
或者直接
boolean b = Pattern.matches(regID16, str);
从字符串中读取每一条匹配的数据
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(string);
while (m.find()) {
strs.add(m.group());
}