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
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
((\d+-\d+),)*(\d+-\d+)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
^((\d+-)?\d+),*((\d+-)?\d+ )$
@enachemc - you RE doesn't match: 1 or 1,2,3
@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
@HonorGod
author said they are not valid
author said they are not valid
Ah! I misread that... Thanks for pointing it out. Please forgive me.
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
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
^((\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
And that one is identical to what enachemc provided in https://www.experts-exchange.com/questions/26646667/Regular-Expression-for-comma-range.html?anchorAnswerId=34237012#a34237012, above
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
argh...
Q-26646667.jpg
Q-26646667.jpg
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
Thanks
ASKER
My solution works on some, and appear that alt are also valid in some cases.
Please give enachemc (answer https://www.experts-exchange.com/questions/26646667/Regular-Expression-for-comma-range.html?anchorAnswerId=34237012#a34237012 ) most of the credit!
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
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