Link to home
Start Free TrialLog in
Avatar of GMSDev
GMSDev

asked on

Regular Expression for comma range

I am having troble putting together a regex that .NET will evaluate to validate page ranges.  The user can enter a set of page ranges to print in the form:

start-end,start2-end2 etc.  

Here are some legal examples:

1-1
1-2,3-4
1-2,3-4,4-5

singletons are not allowed.  These are invalid:

1,2-3
1,
1
1-2,3

I tried this expression (\d-\d)([,]\d-\d)*  but it is too greedy and things like 1-1, still match.  Can anyone help me with a valid expression?

Thanks
Avatar of enachemc
enachemc
Flag of Afghanistan image

((\d+-\d+),)*(\d+-\d+)
SOLUTION
Avatar of enachemc
enachemc
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HonorGod
^((\d+-)?\d+),*((\d+-)?\d+)$

@enachemc - you RE doesn't match:  1  or  1,2,3


^    = Beginning of line
(    = Beginning of group
(    = Beginning of group
\d+- = 1 or more digits followed by a dash (hyphen)
)    = End of group
?    = Allow preceding group zero or 1 times
\d+  = 1 or more digits
)    = End of group
*    = Allow preceding group zero or more times
(    = Beginning of group
(    = Beginning of group
\d+- = 1 or more digits followed by a dash (hyphen)
)    = End of group
?    = Allow preceding group zero or 1 times
\d+  = 1 or more digits
)    = End of group
$    = End of string / line

Open in new window

@HonorGod

author said they are not valid
Almost ;-)

^(((\d+-)?\d+),)*((\d+-)?\d+)$

Verified at: http://regexpal.com/
Ah!  I misread that... Thanks for pointing it out.  Please forgive me.
Avatar of GMSDev
GMSDev

ASKER

Hi All,

Thank you for all the feedback, but I am unable to get any of these expressions to work.  I am using the following test cases, but none of them match in my code, on regexpal.com matcher or on my validation site.

1-1
1-2,3-4
1-2,3-4,4-5


regex.gif
This one works for me on http://regexpal.com/

^((\d+-\d+),)*(\d+-\d+)$

Match:
1-1
1-2,3-4
1-2,3-4,4-5

Fail:
1,2-3
1,
1
1-2,3
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GMSDev

ASKER

I tried exactly like you did, but still didn't get the same results.  I wonder if it is a difference in browsers.  I am using I.E. 8.  Anyways, I appreaciate the help.

Thanks

Avatar of GMSDev

ASKER

My solution works on some, and appear that alt are also valid in some cases.
If you are using the pattern in .NET, why don't you use a .NET-compatible tester? For all intents and purposes, that pattern should be simple enough to evoke the same behavior in most engines, but since you are putting it into .NET anyways, why not test it in a small sample project or something like Expresso or http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx .
All,
 
Following an 'Objection' by HonorGod (at https://www.experts-exchange.com/questions/26648601/04-Dec-10-14-Automated-Request-for-Review-Objection-to-Split-Q-26646667.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
_alias99
Community Support Moderator
Thanks & Good luck