我试图使用正则表达式执行某个代码。在下面的示例中,我的目的是让名称中的所有项目匹配,如果在name3中找到并打印出来。问题是只打印出匹配的字符串中的第一项。
import re
def foo():
for counter, option in enumerate(keywords):
try:
name = 'can one and three be odd numbers'
name2 = name[:]
name3 = ['one','two','three', 'four',]
regex1one = re.compile(r'\b(?:%s)\b'%'|'.join(name3))
regex11one = re.compile(r'\b(?:%s)\b'%'|'.join(keywords))
name4 = regex1one.search(name).group()
print name4
break
except:
break
keywords = ['be','are','is',]
foo()
答案 0 :(得分:0)
感谢tobias_k,我删除了所有无用的东西。这是:
导入重新
def foo():
name = 'can one and three be odd numbers'
name3 = ['one','two','three', 'four',]
regex1one = re.compile(r'\b(?:%s)\b'%'|'.join(name3))
regex11one = re.compile(r'\b(?:%s)\b'%'|'.join(keywords))
name4 = re.findall(regex1one, name)
print name4
keywords = ['be','are','is',]
foo()