如何匹配正则表达式中的多个单词

时间:2022-09-13 11:41:48

Just a simple regex I don't know how to write.

只是一个简单的正则表达式,我不知道如何写。

The regex has to make sure a string matches all 3 words. I see how to make it match any of the 3:

正则表达式必须确保字符串匹配所有3个单词。我看到如何使它匹配3中的任何一个:

/advancedbrain|com_ixxocart|p\=completed/

but I need to make sure that all 3 words are present in the string.

但我需要确保字符串中包含所有3个单词。

Here are the words

这是单词

  1. advancebrain
  2. advancebrain
  3. com_ixxocart
  4. com_ixxocart
  5. p=completed
  6. P =完成

3 个解决方案

#1


16  

Use lookahead assertions:

使用前瞻断言:

^(?=.*advancebrain)(?=.*com_ixxochart)(?=.*p=completed)

will match if all three terms are present.

如果所有三个术语都存在,则匹配。

You might want to add \b work boundaries around your search terms to ensure that they are matched as complete words and not substrings of other words (like advancebraindeath) if you need to avoid this:

您可能希望在搜索词周围添加\ b工作边界,以确保它们匹配为完整的单词而不是其他单词的子串(如advancebraindeath),如果您需要避免这种情况:

^(?=.*\badvancebrain\b)(?=.*\bcom_ixxochart\b)(?=.*\bp=completed\b)

#2


4  

^(?=.*?p=completed)(?=.*?advancebrain)(?=.*?com_ixxocart).*$

Spent too long testing and refining =/ Oh well.. Will still post my answer

花了太长时间测试和精炼= /哦好..仍然会发布我的答案

#3


1  

Use lookahead:

使用前瞻:

(?=.*\badvancebrain)(?=.*\bcom_ixxocart)(?=.*\bp=completed)

Order won't matter. All three are required.

订单无关紧要。这三个都是必需的。

#1


16  

Use lookahead assertions:

使用前瞻断言:

^(?=.*advancebrain)(?=.*com_ixxochart)(?=.*p=completed)

will match if all three terms are present.

如果所有三个术语都存在,则匹配。

You might want to add \b work boundaries around your search terms to ensure that they are matched as complete words and not substrings of other words (like advancebraindeath) if you need to avoid this:

您可能希望在搜索词周围添加\ b工作边界,以确保它们匹配为完整的单词而不是其他单词的子串(如advancebraindeath),如果您需要避免这种情况:

^(?=.*\badvancebrain\b)(?=.*\bcom_ixxochart\b)(?=.*\bp=completed\b)

#2


4  

^(?=.*?p=completed)(?=.*?advancebrain)(?=.*?com_ixxocart).*$

Spent too long testing and refining =/ Oh well.. Will still post my answer

花了太长时间测试和精炼= /哦好..仍然会发布我的答案

#3


1  

Use lookahead:

使用前瞻:

(?=.*\badvancebrain)(?=.*\bcom_ixxocart)(?=.*\bp=completed)

Order won't matter. All three are required.

订单无关紧要。这三个都是必需的。