• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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
0
GMSDev
Asked:
GMSDev
  • 9
  • 4
  • 3
  • +2
3 Solutions
 
enachemcCommented:
((\d+-\d+),)*(\d+-\d+)
0
 
enachemcCommented:
^((\d+-\d+),)*(\d+-\d+)$

^ for start of line
$ for end of line
0
 
HonorGodCommented:
^((\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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
enachemcCommented:
@HonorGod

author said they are not valid
0
 
HonorGodCommented:
Almost ;-)

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

Verified at: http://regexpal.com/
0
 
HonorGodCommented:
Ah!  I misread that... Thanks for pointing it out.  Please forgive me.
0
 
GMSDevAuthor Commented:
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
0
 
HonorGodCommented:
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
0
 
HonorGodCommented:
And that one is identical to what enachemc provided in http://e-e.com/Q_26646667.html#a34237012, above
0
 
GMSDevAuthor Commented:
I still can't get that to work, maybe you could share a screen shot.

I did get this to work and seems to be fine:

 ((\d+-\d+))*(,\d+-\d+)*
regex.gif
0
 
HonorGodCommented:
If you check the 2nd checkbox [i.e., ^$ match at line breaks(m) ], you should be all set
0
 
HonorGodCommented:
argh...
Q-26646667.jpg
0
 
GMSDevAuthor Commented:
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

0
 
GMSDevAuthor Commented:
My solution works on some, and appear that alt are also valid in some cases.
0
 
HonorGodCommented:
Please give enachemc (answer http://e-e.com/Q_26646667.html#a34237012 ) most of the credit!
0
 
käµfm³d 👽Commented:
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 .
0
 
_alias99Commented:
All,
 
Following an 'Objection' by HonorGod (at http://www.experts-exchange.com/Q_26648601.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
0
 
HonorGodCommented:
Thanks & Good luck
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now