正则表达式用于某些顺序无关紧要的单词的存在 [英] Regex for existence of some words whose order doesn't matter
问题描述
我想编写一个正则表达式来搜索某些单词的存在,但是它们的出现顺序无关紧要.
I would like to write a regex for searching for the existence of some words, but their order of appearance doesn't matter.
例如,搜索"Tim"和"stupid".我的正则表达式是Tim.*stupid|stupid.*Tim
.但是是否可以编写一个更简单的正则表达式(例如,使两个单词在正则表达式本身中仅出现一次)?
For example, search for "Tim" and "stupid". My regex is Tim.*stupid|stupid.*Tim
. But is it possible to write a simpler regex (e.g. so that the two words appear just once in the regex itself)?
推荐答案
参见此正则表达式:
/^(?=.*Tim)(?=.*stupid).+/
正则表达式说明:
-
^
在字符串开头声明位置. -
(?=.*Tim)
断言字符串中存在"Tim". -
(?=.*stupid)
断言字符串中存在愚蠢". -
.+
现在存在我们的短语,此字符串有效.继续并使用.+
或-.++
来匹配整个字符串.
^
Asserts position at start of string.(?=.*Tim)
Asserts that "Tim" is present in the string.(?=.*stupid)
Asserts that "stupid" is present in the string..+
Now that our phrases are present, this string is valid. Go ahead and use.+
or -.++
to match the entire string.
要更专门地使用前瞻,可以添加另一个(?=.*<to_assert>)
组.整个正则表达式可以简化为/^(?=.*Tim).*stupid/
.
To use lookaheads more exclusively, you can add another (?=.*<to_assert>)
group. The entire regex can be simplified as /^(?=.*Tim).*stupid/
.
请参见正则表达式演示!
>>> import re
>>> str ="""
... Tim is so stupid.
... stupid Tim!
... Tim foobar barfoo.
... Where is Tim?"""
>>> m = re.findall(r'^(?=.*Tim)(?=.*stupid).+$', str, re.MULTILINE)
>>> m
['Tim is so stupid.', 'stupid Tim!']
>>> m = re.findall(r'^(?=.*Tim).*stupid', str, re.MULTILINE)
>>> m
['Tim is so stupid.', 'stupid Tim!']
了解更多信息
这篇关于正则表达式用于某些顺序无关紧要的单词的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!