Regex is always difficult, but this should work for you. A helpful resource to test and hone Regex can be found here: https://pythex.org/
I've done this solution such that you can use it with either a dictionary of phrases or an entire block of text (i.e. a string).
# Python 3.0+
import re
target_string = "oh my god omg oh-my-god oh my god! oh my god! oh my god Oh my god OMG Oh-my-god Oh my god!" \
"Oh my god! Oh my god Oh My God OmG Oh-My-God Oh My God! Oh My god! Oh My God the ggod game game god godohmygod 132 !@#$%^&*()"
# Dictionary of phrases you want to search
dictionary = ['oh my god', 'omg', 'oh-my-god', 'oh my god!', 'oh, my god!' 'oh, my god', 'Oh my god', 'OMG', 'Oh-my-god',
'Oh my god!',
'Oh, my god!' 'Oh, my god', 'Oh My God', 'OmG', 'Oh-My-God', 'Oh My God!', 'Oh, My god!' 'Oh, My God',
'the ggod game', 'game', 'god godohmygod', '132', '!@#$%^&*()']
#Loop through the dictionary and print phrases that matches the regular expression
def match_phrase():
count = 0
for p in dictionary:
regex = re.compile(
r"(?:oh|o|O)(?: |-|,|!|\.)*(?:my|m'y|m|M)*(?: |-|,|!|\.)*(?:god|g-o-d|GOD|g.o.d|God|gOD|GoD|g|G)(?: |-|,|!|\.)*")
if regex.match(p):
print("Matching words in dictionary: ",p)
count = count + 1
print("Matching words in dictionary: ", f'{count}\n')
#Loop through a string of text and return all matching results
def match_text_string():
regex = re.compile(
r"(?:oh|o|O)(?: |-|,|!|\.)*(?:my|m'y|m|M)*(?: |-|,|!|\.)*(?:god|g-o-d|GOD|g.o.d|God|gOD|GoD|g|G)(?: |-|,|!|\.)*")
print("Matching words in target_string: ", len(result))
result = re.findall(regex, len(target_string))
# print the matching word using group() method
print("Matching words in target_string: ", len(result))
if __name__ == '__main__':
match_phrase()
match_text_string()