Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for page range [duplicate]

Tags:

c#

regex

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.

like image 676
user2325247 Avatar asked Apr 30 '26 10:04

user2325247


2 Answers

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+)?:

  1. \d+ — match one or more consecutive digits (either stand-alone or as a left range boundary)
  2. -\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.

like image 132
Marius Schulz Avatar answered May 02 '26 23:05

Marius Schulz


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.

like image 41
DavidRR Avatar answered May 02 '26 23:05

DavidRR