# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:FN:Tom )((.|\n)*)(?:\nEND:VCARD)"
test_str = ("FN:Tom root:x:0:0:root:/root:/bin/bash\n"
"daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n"
"bin:x:2:2:bin:/bin:/usr/sbin/nologin\n"
"sys:x:3:3:sys:/dev:/usr/sbin/nologin\n"
"sync:x:4:65534:sync:/bin:/bin/sync\n"
"games:x:5:60:games:/usr/games:/usr/sbin/nologin\n"
"man:x:6:12:man:/var/cache/man:/usr/sbin/nologin\n"
"lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin\n"
"mail:x:8:8:mail:/var/mail:/usr/sbin/nologin\n"
"news:x:9:9:news:/var/spool/news:/usr/sbin/nologin\n"
"uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin\n"
"proxy:x:13:13:proxy:/bin:/usr/sbin/nologin\n"
"www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin\n"
"backup:x:34:34:backup:/var/backups:/usr/sbin/nologin\n"
"list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin\n"
"irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin\n"
"gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin\n"
"nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin\n"
"systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false\n"
"systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false\n"
"systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false\n"
"systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false\n"
"syslog:x:104:108::/home/syslog:/bin/false\n"
"_apt:x:105:65534::/nonexistent:/bin/false\n"
"messagebus:x:106:110::/var/run/dbus:/bin/false\n"
"uuidd:x:107:111::/run/uuidd:/bin/false\n"
"student:x:1000:1000:student,,,:/home/student:/bin/bash\n"
"sshd:x:108:65534::/var/run/sshd:/usr/sbin/nologin\n\n"
"END:VCARD")
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