import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "K'(?|(?P<name1>81)\\d+|(61)\\d+|(64)\\d+|(1(?:45|33)?)\\d+|(44)\\d+|(86)\\d+|(678)\\d+|(41)\\d+|(49)\\d+|(33)\\d+|(685)\\d+|(\\d{1,3})\\d+)";
final String string = "K'8134567\n"
+ "K'81345678\n"
+ "K'6134516789\n"
+ "K'61345678\n"
+ "K'643456\n"
+ "K'646345678\n"
+ "K'1234567890\n"
+ "K'12345678901\n"
+ "K'1454567890 <<<--- want 145 returned and not 1 \n"
+ "K'13345678901 <<<--- want 133 returned and not 1 \n"
+ "K'3214567890123\n"
+ "K'32134567890123\n"
+ "K'3654567890123\n"
+ "K'8934567890123\n"
+ "K'6554567890123\n\n\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (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