I'm writing a regular expression to parse page numbers and page ranges.
A user can enter page numbers in the following formats:
45
45,47
45-48
45,47-52
45-48,56,59-62,98-102 etc...
So either a single page, a range of pages, pages separated by a comma or a combination.
And I've written the following regex which matches the first 3 cases
Code: Select all
[0-9]{1,3}(-|,)?[0-9]{0,3}Obviously what I could do is just add the (-|,)?[0-9]{0,3} section multiple times after the initial regex, to deal with the multiple cases in the 4th and 5th examples.
But I'm hoping there's an easier way I'm missing out on. Is it valid to nest regular expression statements?
Just tried something like this...
Code: Select all
[0-9]{1,3}((-|,){1}[0-9]{1,3})*It seems to work in Regex Coach (excellent utility BTW), but I wasn't if I was missing something that could improve/shorten the regex?
Cheers, B