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: 
It would be far easier to NOT do this particular problem with a single regular expression.

First off, `\b` is zero-width. So you do not need to follow it using a `?`, your intention is probably `\s?`.

Next, regex is pretty much stateless, in the general case, which means that you'd need to construct your regex as follows.

```
^\s*(one\s*,(two\s*,(three\s*,four|four\s*,three)|three\s*,(two\s*,four|four\s*,two)...

```

As you can see, you have to manually deal with combinatoric explosion. Which is far less than ideal.

You should instead, split on `,` and use java to check.

> 
> Thanks for replying. From what i understand, you want me not use regex rather use java.can you detail a bit on how to check in java
> 
> 
> 

Try this (untested code, will be bugs):

```
public parseList(String input) {
  String[] numbers = { "one", "two", "three", "four" };
  bool foundNumbers = { false, false, false, false };
  String delims = "\s*,";
  String[] tokens = input.split(delims);

  if (tokens.length != 4) {
    //deal with error case as you wish
  }

  for (int i = 0; i < numbers.length; ++i) {
    for (int j = 0; j < tokens.length; ++j) {
      if (numbers[i].equals(tokens[j])) {
        if (!foundNumbers[i]) {
          foundNumbers[i] = true;
        } else {
          //deal with error case as you wish
        }
      }
    }
  }

  for (int i = 0; i < foundNumbers.length; ++i) {
    if (!foundNumbers[i]) {
      //deal with error case as you wish
    }
  }

  //success
}

```
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 :)