示例:
strings_to_search = ['abc', 'def', 'fgh hello']
complete_list = ['abc abc dsss abc', 'defgj', 'abc fgh hello xabd', 'fgh helloijj']
for col_key in strings_to_search:
print(list(map(lambda x: re.findall(col_key, x), complete_list)))
我们通过运行上面的程序获得低于输出,我能够匹配abc 4次,因为它在第0个索引中匹配3次而在complete_list的第2个索引中匹配1次 .
'def'与'defgj'相匹配,但我想只在有'def abc'或'def'这样的字符串时匹配 . (用空格分隔或匹配字符串的开头和结尾)
同样'fgh hello'与'abc fgh hello xabd'和'fgh helloijj'相匹配 . 我希望这只能与'abc fgh hello xabd'匹配,因为它是用空格分隔的 . 任何人都可以建议我如何在python中实现这一点?
[['abc', 'abc', 'abc'], [], ['abc'], []]
[[], ['def'], [], []]
[[], [], ['fgh hello'], ['fgh hello']]
1 回答
在正则表达式中使用分词符(\ b) .
输出: