Mattijs
Mattijs

Reputation: 3430

Regex to validate comma separated year range

I came up with this regex:

(?:[0-9]{4}-([0-9]{4}|[?]))+

for this text:

1993-2000,2004-?

The regex to capture on block = [0-9]{4}-([0-9]{4}|[?])

I have variations like:

1993-?
1993-2000
1993-2000,2004-?
1993-2000,2004-2010

and so on.

My regex captures the blocks [1993-2000] and [2004-?], but not the comma.

What I find hard is to declare the comma should be mandatory for the second, third, etc. occurrences.

So what should not be allowed is:

1993-2000,
1993-20002007-?
?-2000

Possibly this could be allowed to0: 1993-2000,2004,2007,2010-?

Can someone help me get this last understanding about a conditional comma for second and followup occurrences?

I found this regex which I adapted a bit:

^([0-9]{4}(-([0-9]{4}|[?]))?)(,([0-9]{4}(-([0-9]{4}|[?]))?))*$

Seems to do the trick, but is this the best version?

Upvotes: 4

Views: 358

Answers (1)

nhahtdh
nhahtdh

Reputation: 56819

Your regex (the last one) looks good enough. There should be no problem about over-rejecting or over-accepting. There are 2 unnecessary groupings, though:

^[0-9]{4}(-([0-9]{4}|[?]))?(,[0-9]{4}(-([0-9]{4}|[?]))?)*$

You can make all the groups non-capturing, since you are validating and not going to capture any text:

^[0-9]{4}(?:-(?:[0-9]{4}|[?]))?(?:,[0-9]{4}(?:-(?:[0-9]{4}|[?]))?)*$

\d can be used in place of [0-9], but I think it doesn't hurt to declare it clearly.

Upvotes: 2

Related Questions