Get string that was matched by regex?

随声附和 提交于 2019-12-24 02:44:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!