I have a list of say 5 words (foo bar foobar footable somebar). This list of words will be provided as a string each separated by space and in any order. I need a regex matching below requirements
So far I have this
(\b(foo|bar|foobar|footable|somebar)\b\ *){1,5}
Need a solution for the 3rd & 4th requirements
Start with a regular expression that matches exactly 5 words:
^(?:\w+\s+){4}\w+$
The anchors ensure that this matches the entire string.
Then prefix this with lookaheads that match each of the words.
(?=.*\bfoo\b)(?=.*\bbar\b)(?=.*\bfoobar\b)(?=.*\bfootable\b)(?=.*\bsomebar\b)^(?:\w+\s+){4}\w+$