elpaulo
elpaulo

Reputation: 96

Need help on a specific Regex

I want to validate an attribute of an xml file with an XSD file. This attribute must contain a list of comma-separated languages, like this :

<Params languages="English,French,Spanish" /> // OK
<Params languages="French,Spanish" />  // OK
<Params languages="French" /> // OK
<Params languages="English,French,Spanish,French" /> // NOK (Two times French)
<Params languages="English,Spanish,French," /> // NOK (Comma at the end)

So i try to write a regex pattern to validate this. Here are the constraints :

The two last constraints are the problem, i don't know how to do it.

Here is what i got now :

^[English,|French,|Spanish,...]+$ 

But it's really poor. Thanks for your help.

Upvotes: 0

Views: 40

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

The below regex would satisfy all your requirements,

^(English|French|Spanish)(?:,(?!\1)(English|French|Spanish))?(?:,(?!\1|\2)(English|French|Spanish))?$

DEMO

Update:

^(English|French|Spanish|German)(?:,(?!\1)(English|French|Spanish|German))?(?:,(‌​?!\1|\2)(English|French|Spanish|German))?(?:,(?!\1|\2|\3)(English|French|Spanish|‌​German))?$

Upvotes: 1

Related Questions