Skip to content Skip to sidebar Skip to footer

Get String That Was Matched By Regex?

I got this code for a reddit bot: match = re.findall(r'(?i)\bword1\b|\bword2\b|\bword3\b', comment.body) which matches several words. How can I print which word was matched?

Solution 1:

Look at this example. This may helps you

import re
f=open('sample.txt',"w")
f.write("<p class = m>babygameover</p>")
f.close()
f=open('sample.txt','r')
string = "<p class = m>(.+?)</p>"
pattern = re.compile(string)
text = f.read()
search = re.findall(pattern,text)
print search

Post a Comment for "Get String That Was Matched By Regex?"