import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\d{8}[A-Z]\\d{5}";
final String string = "123456789\n"
+ "987654321\n"
+ "987654322\n"
+ "987654323\n"
+ "98765432A12312\n"
+ "987654325\n"
+ "987654326\n"
+ "987654327\n"
+ "987654328\n"
+ "987654329\n"
+ "666218502\n"
+ "773218502\n"
+ "772218502\n"
+ "443218502\n"
+ "641218502\n"
+ "525.52.6969B\n"
+ "515-54-6976-A\n"
+ "555516316S\n"
+ "123.45.6789\n"
+ "987.65.4321\n"
+ "987.65.4322\n"
+ "987.65.4323\n"
+ "987.65.4324\n"
+ "987.65.4325\n"
+ "987.65.4326\n"
+ "987.65.4327\n"
+ "987.65.4328\n"
+ "987.65.4329\n"
+ "666.21.8502\n"
+ "773.21.8502\n"
+ "772.21.8502\n"
+ "772-21-8502\n"
+ "772218502\n"
+ "443.21.8502\n"
+ "641.21.8502";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html