# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?x) (?<!\d) (\d{5} | K\d{4}) (?!\d)"
test_str = ("(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")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html