![](/img/trans.png)
[英]How do I match exact strings from a list to a larger string taking white spaces into account?
[英]match exact string separated by white spaces python
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次,因为它在complete_list的第0个索引中匹配了3次,在第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']]
在正则表达式中使用分词(\\ b)。
import re
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:
word = r'\b{}\b'.format(col_key)
print(list(map(lambda x: re.findall(word, x), complete_list)))
输出:
[['abc', 'abc', 'abc'], [], ['abc'], []]
[[], [], [], []]
[[], [], ['fgh hello'], []]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.