# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?'username'.*):.*:(?'UID'.*):(?'GID'.*):(?'GECOS'.*):(?'home'.*):(?'shell'.*)$"
test_str = ("root:x:0:0:Root User:/root:/bin/bash\n"
"john:x:1001:1001:John Smith,Room 101,+11234567890,,johnsmith@email.com:/home/john:/bin/bash\n"
"jane:x:1002:1002:Jane Doe,Room 102,+442012345678,,jane.doe@email.co.uk:/home/jane:/bin/bash\n"
"mike:x:1003:1003:Michael Johnson,Room 201,+498912345678,,michael.johnson@email.de:/home/mike:/bin/bash\n"
"sarah:x:1004:1004:Sarah Brown,Room 202,+61255512345,,sarah.brown@email.au:/home/sarah:/bin/bash\n"
"karen:x:1005:1005:Karen Miller,Room 203,+12155551234,,karen.miller@email.com:/home/karen:/bin/bash\n"
"jason:x:1006:1006:Jason Lee,Room 204,+81312345678,,jason.lee@email.jp:/home/jason:/bin/bash\n"
"steve:x:1007:1007:Steven Wilson,Room 205,+33123456789,,steven.wilson@email.fr:/home/steve:/bin/bash\n"
"emma:x:1008:1008:Emma Johnson,Room 206,+13455551234,,emma.johnson@email.com:/home/emma:/bin/bash\n"
"olivia:x:1009:1009:Olivia Brown,Room 207,+441234567890,,olivia.brown@email.co.uk:/home/olivia:/bin/bash\n"
"lucas:x:1010:1010:Lucas White,Room 208,+61455512345,,lucas.white@email.au:/home/lucas:/bin/bash\n"
"isabella:x:1011:1011:Isabella Adams,Room 209,+41123456789,,isabella.adams@email.ch:/home/isabella:/bin/bash\n"
"liam:x:1012:1012:Liam Thompson,Room 210,+17345551234,,liam.thompson@email.com:/home/liam:/bin/bash\n"
"noah:x:1013:1013:Noah Scott,Room 211,+17345551235,,noah.scott@email.com:/home/noah:/bin/bash\n"
"ava:x:1014:1014:Ava Clark,Room 212,+61255512346,,ava.clark@email.au:/home/ava:/bin/bash\n"
"sophia:x:1015:1015:Sophia Turner,Room 213,+33123456790,,sophia.turner@email.fr:/home/sophia:/bin/bash")
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