What would be the best regular expression in c# to validate the below condition?
1, 2-10,5-10,6,9-100 - it is something like page numbers specified as range or individual ones separated by commas.
What would be the best regular expression in c# to validate the below condition?
1, 2-10,5-10,6,9-100 - it is something like page numbers specified as range or individual ones separated by commas.
Try the following expression:
\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*
Note that the pattern is quite fragile in that it doesn't allow any whitespace as is.
The idea is built around the main subpattern \d+(?:-\d+)?
:
\d+
— match one or more consecutive digits (either stand-alone or as a left range boundary)-\d+
— match a minus sign followed by one or more digits (right range boundary)The trailing question mark makes the minus sign and the right range boundary optional (which is required to also match single page numbers); the (?:)
denotes a non-capturing group.
When I cut-and-paste your sample string, I noticed that a space follows the 1,
at the very beginning:
1, 2-10,5-10,6,9-100
^
I don't know if that was intentional, but I think it's reasonable to allow one or more space characters to surround a comma.
That said, here's a regex that will meet your requirements:
^[0-9]+(?:(?:\s*,\s*|-)[0-9]+)*$
^^^^^^ ^^^^^^^ ^ ^^^^^^ ^
A B1 B2 C D
^^^^^^^^^
B
A - One or more digits
B1 - A comma with optional space characters on either side, *OR*
B2 - A dash (without whitespace on either side)
C - One or more digits
D - Optionally repeat B and C
Note: \d
and [0-9]
are not equivalent; the former matches all Unicode digits. I have presumed that only the digits 0
through 9
are of interest to you.
This regex will match each page range individually:
\d+-\d+|\d+
I used alternation to accomplish this. In the case that \d+-\d+
(a page number range) is not matched, it will simply match a singular, infinite number \d+
.
If you encounter any characters between the page ranges besides -
, you will need to change the regex.