【发布时间】:2018-08-19 01:22:28
【问题描述】:
是否有一个正则表达式供我们检查字符串中是否存在多个单词
例如:
sentence = "hello i am from New York city"
我想检查句子中是否存在“hello”、“from”和“city”。
我尝试过使用
re.compile("hello|from|city")
但没有运气,因为即使找到一个匹配项,它也会返回 true。
【问题讨论】:
-
我不知道 python,但你可以尝试类似
(?=hello)(?=from)(?=city)在 perl 中工作的东西 -
@mankowitz 这行不通,因为如果一个位置紧跟在
hello之后,那么from也不一定紧跟它。 -
对不起:
(?=.*hello)(?=.*from)(?=.*city)
标签: python regex python-2.7 pattern-matching