import re
regex = re.compile(r"(?<=[,\s:]|^)([\wа-я\-]+)(?=[:,.\s]|$)", flags=re.MULTILINE)
test_str = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n"
"sed do eiusmod tempor incididunt ut labore et dolore magna\n"
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation\n"
"ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
"Duis aute irure dolor in reprehenderit in voluptate velit\n"
"esse cillum dolore eu :fugiat: nulla pariatur. Excepteur sint\n"
"occaecat :cupidatat [non] proident:, sunt in culpa qui officia\n"
"deserunt mollit anim id est laborum.\n\n"
"abcdefghijklmnopqrstuvwxyz [ABCDEFGHIJKLMNO] PQRSTUVWXYZ\n"
"0123456789 _+-.,!@#$%^&*();\\/|<>\"'\n"
"12345 -98.7 3.141 .6180 9,000 +42\n"
"555.123.4567 +1-(800)-555-2468\n"
"foo@demo.net <bar.ba@test.co.uk>\n"
"www.demo.com {http://foo.co.uk http } \n"
"[https://marketplace.visualstudio.com/ite] ms?itemName=chrmarti.regex\n"
"https://github.com/chrmarti/vscode-regex asdfasdf\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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