[英]Regex check if specific multiple words present in a sentence
Is there a regex for us to check if multiple words are present in a string 是否有正则表达式供我们检查字符串中是否存在多个单词
Ex : 例如:
sentence = "hello i am from New York city"
I want to check if 'hello' 'from' and 'city' are present in sentence. 我想检查句子中是否存在“ hello”,“ from”和“ city”。
I have tried using 我尝试使用
re.compile("hello|from|city")
but no luck as it returns true if even a single match is found. 但没有运气,因为即使找到一个匹配项,它也都返回true。
You can't alternate, because then a match for any of the alternations would fulfill the regex. 您不能替代,因为任何替代的匹配都将满足正则表达式。 Instead, use multiple lookaheads from the start of the string: 相反,请从字符串开头使用多个前行:
sentence1 = "hello i am from New York city"
sentence2 = "hello i am from New York"
regex = re.compile(r"^(?=.*hello)(?=.*from)(?=.*city)")
print(regex.match(sentence1))
print(regex.match(sentence2))
Output: 输出:
<_sre.SRE_Match object; span=(0, 0), match=''>
None
You can do this without using regex, just checking entrance of each word (from words
) in sentence
: 您无需使用正则表达式即可执行此操作,只需检查sentence
中每个单词(从words
)的进入即可:
sentence = "hello i am from New York city"
words = ['hello', 'from', 'city']
all([w in sentence.split() for w in words])
In my opinion, this way is preferable because of clarity. 我认为,由于清晰起见,这种方式是可取的。
Try: 尝试:
>>> sentence = "hello i am from New York city"
>>> def f(s):
return all(s.split().__contains__(i) for i in ['hello','from','city'])
>>> f(sentence)
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.