Wednesday, May 16, 2012

Regex validate sequence

I've a sequence of comma separated words like x1,x2,x3.... Some words in this sequence maybe optional i.e the sequence x1,x3,x4,x6 is valid with x2 and x5 being optional, and the rest of the words being mandatory. The above sequence can be repeated for maximum of n number of times.

For ex, the sequence of words x1,x2,x3 can be repeated maximum of 3 times with x2 being optional, the following is a valid sequence: x1,x3,x1,x2,x3,x1,x3

How do I validate the above using a regex pattern?

I tried something like
But the above will work for all multiples of three, if I use {1,3} then any amount of repetitions will match because they're multiples of 1 {1,3}.

No comments:

Post a Comment