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())