mardi 2 février 2016

Can Regex do conditional values?

I have a String being generated by concatenating a set of String with a comma delimeter. Now I want to write a unit test covering this code, I want to check that all Strings in the set made it into the concatenated String. The problem is that sets are not ordered, so I can't know for sure what the concatenated String will be. And I can't change the Set to an ordered Set or a List as I don't own that bit of code.

As an example, if my set was {"VAL1", "VAL2"}, my test currently looks like this:

assertTrue("VAL1,VAL2".equals(concString) || "VAL2,VAL1".equals(concString));

This is fine, but if my set had 5, or even 10 values, this will become impractical. So I considered changing it to:

assertTrue("VAL[1-2],VAL[1-2]".matches(concString));

However this could also match the incorrect case "VAL1,VAL1". Is there a way in regex to say "use this set of values, but don't match a value that was matched already"?

Aucun commentaire:

Enregistrer un commentaire