Say I have a query like this: "one two three", if I replace with spaces with | (pipe character) I can match a string if it contains one or more of those words. This is like a logical OR.
Is there something similar that does a logical AND. It should match regardless of word ordering as long as all the words are present in the string.
Unfortunately I'm away from my Mastering Regular Expressions book :(
Edit: I'm using Javascript and the query can contain any amount of words.
Try look-ahead assertions:
(?=.*one)(?=.*two)(?=.*three)
But it would be better if you use three separate regular expressions or simpler string searching operations.
There's nothing really good for that. You could fairly easily match on three occurrences of any of the words involved:
(?:\b(?:one|two|three)\b.*){3}
but that matches "one one one" as easily as "one two three".
You can use lookahead assertions like Gumbo describes. Or you can write out the permutations, like so:
(?\bone\b.*\btwo\b.*\bthree\b|\btwo\b.*\bone\b.*\bthree\b|\bone\b.*\bthree\b.*\btwo\b|\bthree\b.*\bone\b.*\btwo\b|\bthree\b.*\btwo\b.*\bone\b|\btwo\b.*\bthree\b.*\bone\b)
which is obviously horrible.
Long story short, it's a lot better to do three separate matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With