【发布时间】:2018-12-22 21:06:57
【问题描述】:
我想使用 RegEx 以任意顺序查找字符串中出现一组单词的第一个序列。
例如,如果要查找单词hello、my 和world,则:
- 对于
hello my sweet world,表达式将匹配hello my sweet world; - 对于
oh my, hello world,它将匹配my, hello world; - 对于
oh my world, hello world,它将匹配my world, hello; -
hello world将没有匹配项。
经过一番研究,我尝试了表达式(?=.*?\bhello\b)(?=.*?\bmy\b)(?=.*?\bworld\b).*,它并没有解决我的问题,因为如果所有单词都存在,它会匹配整个字符串,如下所示:
- 对于
oh my world, hello world,它匹配oh my world, hello world
实现我所描述的合适的表达方式是什么?
(虽然 RegEx 是我的程序的首选方法,但如果您认为不可行,欢迎使用任何其他 python 解决方案。)
【问题讨论】: