import re
regex = re.compile(r"(?(DEFINE)(?<urlPart>[^\\\s]+))\\\\(?&urlPart)\\(?&urlPart)\\\K(?&urlPart)", flags=re.MULTILINE)
test_str = ("### Positive matches\n"
"\\\\10.20.3.23\\S$\\COMPANY_NAME\\Main_5e08a942f39a430db0b081736a3f1881\\C_VOL-b002.spf\n"
"\\\\localhost\\part1\\selectMe\n"
"\\\\a\\b\\select-me but not me\n"
"\\\\a\\b\\select_me\\c\\d\\e.abc more text here\n"
"This is the link \\\\a\\b\\select-me\\c\\d\\e.abc inside a longer string\n"
"\\\\a\\b\\select_me_from_1st_link\\c.abc \\\\a\\b\\selectMeFrom2ndLink\\c.abc\n\n"
"## Negative matches\n"
"\\a\\b\\c\\dontSelectMe\\e\\f\n"
"\\\\\\\\dont-select-me or me\n"
"\\\\a \\b \\dont_select_me \\c \\d \\e.abc")
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