繁体   English   中英

正则表达式检查句子中是否存在特定的多个单词

[英]Regex check if specific multiple words present in a sentence

是否有正则表达式供我们检查字符串中是否存在多个单词

例如:

sentence = "hello i am from New York city"

我想检查句子中是否存在“ hello”,“ from”和“ city”。

我尝试使用

re.compile("hello|from|city")

但没有运气,因为即使找到一个匹配项,它也都返回true。

您不能替代,因为任何替代的匹配都将满足正则表达式。 相反,请从字符串开头使用多个前行:

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

输出:

<_sre.SRE_Match object; span=(0, 0), match=''>
None

您可以使用内置的all()方法。

这里的文件

有效地,该函数采用iterable类型作为参数。

例:

words = ["hello", "from", "city"]
if all(word in 'hello from the city' for word in words):
  # Do Something

您无需使用正则表达式即可执行此操作,只需检查sentence中每个单词(从words )的进入即可:

sentence = "hello i am from New York city"
words = ['hello', 'from', 'city']
all([w in sentence.split() for w in words])

我认为,由于清晰起见,这种方式是可取的。

尝试:

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

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