Regex equivalent to XOR

1594 views regex
-1

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!

answered question

Which regex program do you use

Hi Poul, I am using this regex to filter allowable content on a Splunk dashboard. So far I've been using regex101.com to check my regexes.

Could you please provide possible strings for processing? It's not clear how is the NONE can be combined (if it can) with other values

Hi Aleksandr. In this case here the client can enter the category of their product...So the product category could be FOO|BAR or FOO|BAR|SPAM or SPAM but once they enter NONE, meaning that it doesnt fall in any categoy, they wont be allowed to enter any other values. So NONE should always be by itself. I hope I made it a little more clear. Thank you!

So the whole string is either NONE or one of FOO, BAR, SPAM?

1 Answer

7

You can use this regex:

(?!.*NONE)(?:^.*(FOO|BAR|SPAM))*

It starts by a negative looking ahead for: any number of any char followed by 'NONE'.

If this test fails (NONE found) then the match will be empty. 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.

posted this

Have an answer?

JD

Please login first before posting an answer.