Regular expression "all three in any order and nothing more"
I need to make a reg expression that matches the following conditions:
1) mathematics only if it has three words 2) not separated or separated by semicolons (;) 3) in any order 4) all words must be included, otherwise it will not match
I tried the following:
^(?=(.*;|)one)(?=(.*;|)two)(?=(.*;|)three).*$
but somehow it combines options like oneasfafasfsaf, two, three is wrong
please, help!
PS sometimes you need to have more than three, but I want to understand the essence
+3
source to share
2 answers
You can grab all 3 and check if each of them exists if the string is only 3 words long:
^(?=.*?(one))(?=.*?(two))(?=.*?(three))(?:(?:\1|\2|\3);?){3}$
See test at regex101.com (explained on the right); Regular Expression Frequently Asked Questions
+5
source to share