获取正则表达式匹配的字符串?

时间:2017-09-15 12:20:05

标签: python-3.x praw

我为reddit bot获取了这段代码:

match = re.findall(r"(?i)\bword1\b|\bword2\b|\bword3\b", comment.body)

匹配几个单词。如何打印匹配的单词?

1 个答案:

答案 0 :(得分:1)

看看这个例子。这可能会对你有所帮助

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