# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\d+)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\s+(\d+)\s+([A-z\d]+)(\(OK\))?\s+((\w+[A-z]\w+\s+)+)"
test_str = ("-------------------------------------------------- \n"
"0 127.0.0.1:1234 16 739casd1b747171ee9038fdbeb5440f(OK) Hans Dieter\n"
"1 127.0.0.1:1234 16 c196c8077727asd7d2bc691e109904dc(OK) House P \n"
"2 127.0.0.1:1234 15 9647c2fd98asdb7c7278e820bef701c(OK) EisFrau \n"
"3 127.0.0.1:1234 16 ed018ads96a17d55c62d4b6814(OK) [S.L]EisMann \n"
"4 127.0.0.1:1234 0 1f7b7fasd0bc3ce1c300a451b0d039(OK) 0MasterCore0 \n"
"5 127.0.0.1:1234 78 dbf18b744dasd326e3585f4b11d4f40(OK) Abdul (6 users in total)\"")
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