import re
regex = re.compile(r"(?i)(\/| |^)r *\/ *(?!(superstonk( |$|\/)|gme( |$|\/)|all( |$|\/)))\w*(\?|\/|\.| |$|!)", flags=re.MULTILINE)
test_str = ("r/all\n"
"r/ALL\n\n"
"r \n\n"
"r/superstonk\n"
"R/SUPERSTONK\n"
"Word r/superstonk\n"
"word r/superstonk word\n"
"r/superstonk?\n"
"r/superstonk.\n"
"r/superstonk!\n"
"r/superstonk \n"
"r /superstonk\n"
"https://www.reddit.com/r/Superstonk?utm_medium=android_app&utm_source=share\n\n\n\n"
"r/random\n"
"R/RANDOM\n"
"Word r/random\n"
"word r/random word\n"
"r/random?\n"
"r/superstonk.\n"
"r/random!\n"
"r/random \n"
"r /random \n"
"https://www.reddit.com/r/random?utm_medium=android_app&utm_source=share\n\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