简体   繁体   English

匹配由空格python分隔的确切字符串

[英]match exact string separated by white spaces python

Example: 例:

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

We get below output by running the above program, I am able to match abc 4 times as it is matching 3 times in 0th index and 1 time in 2nd index of the complete_list. 通过运行上面的程序,我们得到以下输出,我能够匹配abc 4次,因为它在complete_list的第0个索引中匹配了3次,在第2个索引中匹配了1次。

'def' is matching against 'defgj', but I want to match only if there is a string like 'def abc' or 'def'. 'def'与'defgj'匹配,但是我只想在有'def abc'或'def'之类的字符串时匹配。 (either separated by white-spaces or matching start and end of the string) (由空格分隔或匹配字符串的开始和结束)

similarly 'fgh hello' is matching against 'abc fgh hello xabd' and 'fgh helloijj'. 同样,“ fgh hello”与“ abc fgh hello xabd”和“ fgh helloijj”匹配。 I wanted this to match only against 'abc fgh hello xabd' as it is separated with white-space. 我希望它只与'abc fgh hello xabd'相匹配,因为它用空格分隔。 Can anyone please suggest how I can achieve this in python? 谁能建议我如何在python中实现这一目标?

[['abc', 'abc', 'abc'], [], ['abc'], []]

[[], ['def'], [], []]

[[], [], ['fgh hello'], ['fgh hello']]

Use word breaks (\\b) in your regular expression. 在正则表达式中使用分词(\\ 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)))

Output: 输出:

[['abc', 'abc', 'abc'], [], ['abc'], []]
[[], [], [], []]
[[], [], ['fgh hello'], []]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM