# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"keyword_token:\s*(.*?)\s*(\d[a-zA-Z\d-]*)\(([^()]*)\)"
test_str = ("# streetname 1() refers to house number 1 with an empty () additional qualifier \n\n"
"keyword_token: street name 4()\n"
"keyword_token: street name 4a()\n"
"keyword_token: street name 4Aa()\n"
"keyword_token: street-name 14()\n\n"
"keyword_token: streetname 123()keyword_token: streetname 123()\n"
"# why is it logged one message per line, but we get the address logged twice - sometimes??\n\n"
"keyword_token: streetname 9(7)keyword_token: streetname 9(7)\n"
"keyword_token: streetname 27()\\r\\n a lot more text and log messages in the free form text log - one messageper line \\n\n"
" \n"
"keyword_token: street-name 1-23(BLOCK D HAUS 6)keyword_token: street-name 1-23(BLOCK H HAUS 2)keyword_token: street-name 1-23(BLOCK G HAUS 3)',\n"
" \n"
" \n"
"keyword_token: Streetname 96-98(13)")
matches = re.finditer(regex, test_str, re.MULTILINE)
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