[英]Match all words of list in string using regex or any other way
I'm writing a python script where i have to match all words given in list with the string.我正在编写一个 python 脚本,我必须将列表中给出的所有单词与字符串匹配。 The list could be as long as possible.该列表可以尽可能长。 But i found operations that will match any of the character but couldn't find operation to match all words in list.但是我找到了匹配任何字符的操作,但找不到匹配列表中所有单词的操作。 For example例如
s = "This is a sample string"
list = ["is", "sample"]
// any operation such that re.search(r'',s) return correct result
// I want that regular expression or approach to do it.
Something like this?像这样的东西?
import re
string = "This is a sample string"
lst = ["is", "sample"]
for item in lst:
rx = re.compile(r"\b{}\b".format(item))
if rx.search(string):
print("'{}' is in the string".format(item))
This yields这产生
'is' is in the string
'sample' is in the string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.