简体   繁体   English

正则表达式存在一些其顺序无关紧要的单词

[英]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. 我想写一个正则表达式来搜索某些单词的存在,但它们的出现顺序无关紧要。

For example, search for "Tim" and "stupid". 例如,搜索“Tim”和“stupid”。 My regex is Tim.*stupid|stupid.*Tim . 我的正则表达是Tim.*stupid|stupid.*Tim But is it possible to write a simpler regex (eg so that the two words appear just once in the regex itself)? 但是有可能编写一个更简单的正则表达式(例如,这样两个单词在正则表达式中只出现一次)?

See this regex: 看到这个正则表达式:

/^(?=.*Tim)(?=.*stupid).+/

Regex explanation: 正则表达式解释:

  • ^ Asserts position at start of string. ^在字符串开头处断言位置。
  • (?=.*Tim) Asserts that "Tim" is present in the string. (?=.*Tim)断言字符串中存在“Tim”。
  • (?=.*stupid) Asserts that "stupid" is present in the string. (?=.*stupid)断言字符串中存在“愚蠢”。
  • .+ Now that our phrases are present, this string is valid. .+现在我们的短语存在,这个字符串是有效的。 Go ahead and use .+ or - .++ to match the entire string. 继续使用.+或 - .++来匹配整个字符串。

To use lookaheads more exclusively, you can add another (?=.*<to_assert>) group. 要更独特地使用先行,可以添加另一个(?=.*<to_assert>)组。 The entire regex can be simplified as /^(?=.*Tim).*stupid/ . 整个正则表达式可简化为/^(?=.*Tim).*stupid/

See a regex demo ! 一个正则表达式演示

>>> 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!']

Read more: 阅读更多:

You can use Positive Lookahead to achieve this. 您可以使用Positive Lookahead来实现此目的。 The lookahead approach is nice for matching strings that contain both substrings regardless of order. 前瞻方法很适合匹配包含两个子串的字符串而不管顺序如何。

pattern = re.compile(r'^(?=.*Tim)(?=.*stupid).*$')

Example : 示例

>>> s = '''Hey there stupid, hey there Tim
Hi Tim, this is stupid
Hi Tim, this is great'''
...
>>> import re
>>> pattern = re.compile(r'^(?=.*Tim)(?=.*stupid).*$', re.M)
>>> pattern.findall(s)

# ['Hey there stupid, hey there Tim', 'Hi Tim, this is stupid']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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