问题
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:
/advancedbrain|com_ixxocart|p\=completed/
but I need to make sure that all 3 words are present in the string.
Here are the words
- advancebrain
- com_ixxocart
- p=completed
回答1:
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)
回答2:
^(?=.*?p=completed)(?=.*?advancebrain)(?=.*?com_ixxocart).*$
Spent too long testing and refining =/ Oh well.. Will still post my answer
回答3:
Use lookahead:
(?=.*\badvancebrain)(?=.*\bcom_ixxocart)(?=.*\bp=completed)
Order won't matter. All three are required.
来源:https://stackoverflow.com/questions/5421952/how-to-match-multiple-words-in-regex