I am trying to come up with a regex that would accept any of the values below combined, except when NONE is present. Something similar to an XOR where if NONE is present nothing else would be accepted, and vice-versa.
This is what I have so far...but it doesn't really enforces the mutual exclusivity of NONE:
(NONE)|((FOO|BAR|SPAM)( ?\| ?(FOO|BAR|SPAM))*)
Any suggestions?
Thank you!
You can use this regex:
^(?!.*NONE)(?:.*(FOO|BAR|SPAM))*
It starts by a negative look ahead
for: any number of any char followed by 'NONE'
.
If this test fails (NONE found) then the match will fail. If no 'NONE'
is found, it moves on and matches: From start of string any number of any char followed by any of your Words. This part is repeated.
Edit
to allow NONE by itself:
^NONE$|^(?!.*NONE)(?:.*(FOO|BAR|SPAM))*
It now start by checking if 'NONE' is alone (which is ok).