问题
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?
回答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
来源:https://stackoverflow.com/questions/46239445/get-string-that-was-matched-by-regex