import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?x) (?<!\\d) (\\d{5} | K\\d{4}) (?!\\d)";
final String string = "(1) | 1 | 2018/ID11298 00000012345 PersoNR: 889899 Bridgestone BNPN\n"
+ "(2) | 3 | Kompo 32280EP ###Baukasten### 3789936690 ID PFK Carbon0\n"
+ "(3) | 2 | 20613, 20614, Mietop Antragsnummer C300Coup IVS 33221 ABF\n"
+ "(4) | 2 | Q21009 China lokal produzierte Derivate f/Radverbund 991222 VV\n"
+ "(5) | 6 | ID:61953 F-Pace Enfantillages (Machine arriere) VvSKPMG Lyon09\n"
+ "(6) | 2 | 2017/22222 22222 21895 Einzelkostenprob. 28932 ZürichMP KOS\n"
+ "(7) | K | ID:K1245 Panamera Nitsche Radlager Derivativ Bayreumion PwC\n"
+ "(8) | 7 | LaunchSupport QBremsen BBG BFG BBD 70142,70119 KK 70142";
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