开发者

How to match multiple words in regex

Just a simple reg开发者_如何学运维ex 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:

/advancedbrain|com_ixxocart|p\=completed/

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

Here are the words

  1. advancebrain
  2. com_ixxocart
  3. p=completed


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:

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


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

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


Use lookahead:

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

Order won't matter. All three are required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜