python3 用着则re.match匹配一段话中是否有多个关键词(非必须同时存在,至少命中一个即可)

keys = dog , person
str1 = i have a dog
str2 = one person have a dong
str3 = there is nothing

匹配到符合条件的str并输出key,用re.match可以实现吗

可以


import re
'''
match()函数只检测RE是不是在string的开始位置匹配,

search()会扫描整个string查找匹配;

也就是说match()只有在0位置匹配成功的话才有返回,

如果不是开始位置匹配成功的话,match()就返回none。
'''

keys = ['dog' ,'person']
str1 = 'i have a dog'
str2 = 'one person have a dong'
str3 = 'there is nothing'

r=re.compile(r"dog|person")
res = r.search(str2)

print(res.group())