Question: I'm working on a regular expression where I need to verify a input text that contains 4 distinct words separated by a comma. 

> 
> input text words: - one, two, three, four. Each of these words should not be repeated more than once. 
>  so, it can be: two, three, four, one or three, four, two, one and not one, one, one, one
> 
> 
> 

Here is what I wrote and found a partial solution by doing separate searches

```
^\b(one|two|three|four)\b?,\b(one|two|three|four)\b?,\b(one|two|three|four)\b?,\b(one|two|three|four)\b

```

But the problem with solution is words are getting repeated, and the test for "one, one, one, one" fails.

Can you please let me know how to avoid the duplicates and where am I doing a mistake?

Answer: 
```
boolean valid( String input ) {
  input.tokenize( ',' ).with { list ->
    list.unique( false ) == list &&
      list.every { it in ['one','two','three','four'] }
  }
}

```

should do it without regular expressions
You should definitely not use a regex for this. You can, though:

```
boolean foundMatch = subjectString.matches(
    "(?x)                            # Verbose regex                        \n" +
    "(?:                             # Match...                             \n" +
    " (?:one()|two()|three()|four()) #  one of the four words               \n" +
    " (?:\\s*,\\s*|\\s*$)            #  a comma or end-of-string            \n" +
    "){4}                            # four times                           \n" +
    "$                               # End of string                        \n" +
    "\\1\\2\\3\\4                    # Assert that all four words have matched");

```

The empty capturing groups ensure (together with `\1\2\3\4` at the end) that each word participates exactly once in the match :)