import re
regex = re.compile(r"((.*?)(%20){1,}){2}", flags=re.MULTILINE)
test_str = ("SHOULD MATCH:\n"
"https://domain.com/search=any%20url%20encoded_word-here\n"
"https://domain.com/search=any%20url%20encoded_word-here%20\n"
"https://domain.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty\n"
"https://domain.com/search=z%C5%82oty%20z%C5%82ota%20%C5%82ata\n"
"https://domain.com/search=any%20%20word%20%20here\n"
"https://domain.com/search=any%20word%20here&color=blue\n"
"https://domain.com/search=any-1st%20word_2nd%20here3\n\n"
"SHOULD NOT MATCH:\n"
"https://domain.com/search=one%20two%20three%20four\n"
"https://domain.com/search=one%20%20two%20%20three%20%20four\n"
"https://domain.com/search=one%20%20two%20three%20%20four\n"
"https://domain.com/search=one%20%20two%20%20three%20%20four\n"
"https://domain.com/search=one%20two%20three%20four&color=blue\n"
"https://domain.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty%20word\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