0

so i am trying to come up with a perfect regex for checking a set of predefined string separated by commas without any repeat. Currently i've been able to achive this

^((IN|THE|BUSH|COM|NET|ALL)\s*[,]{0,1}\s*)+$

its works fine for its purpose apart from the fact that its doesn't check for unique values

example is "IN, THE, COM" without quotes this is a correct format

but having this is incorrect

"IN, COM, THE, COM"

i need a regex that works with java for this same task

Jens
  • 63,364
  • 15
  • 92
  • 104

1 Answers1

0

Maybe you can use this regex which uses a negative lookahead (?!.*\1) the check if what is following is not what is captured in the first group \1

^(?:(IN|THE|BUSH|COM|NET|ALL)\h*,?\h*(?!.*\1))+$

The fourth bird
  • 127,136
  • 16
  • 45
  • 63