Solved

Regular Expression for comma range

Posted on 2010-11-30
20
331 Views
Last Modified: 2012-05-10
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
Comment
Question by:GMSDev
  • 9
  • 4
  • 3
  • +2
20 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 34237009
((\d+-\d+),)*(\d+-\d+)
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 400 total points
ID: 34237012
^((\d+-\d+),)*(\d+-\d+)$

^ for start of line
$ for end of line
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34237473
^((\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
 
LVL 12

Expert Comment

by:enachemc
ID: 34237489
@HonorGod

author said they are not valid
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34237493
Almost ;-)

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

Verified at: http://regexpal.com/
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34237495
Ah!  I misread that... Thanks for pointing it out.  Please forgive me.
0
 

Author Comment

by:GMSDev
ID: 34242712
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 34242788
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 34242824
And that one is identical to what enachemc provided in http://e-e.com/Q_26646667.html#a34237012, above
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Accepted Solution

by:
GMSDev earned 0 total points
ID: 34242931
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
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 100 total points
ID: 34242998
If you check the 2nd checkbox [i.e., ^$ match at line breaks(m) ], you should be all set
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34243017
argh...
Q-26646667.jpg
0
 

Author Comment

by:GMSDev
ID: 34243140
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
 

Author Comment

by:GMSDev
ID: 34243173
My solution works on some, and appear that alt are also valid in some cases.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34243196
Please give enachemc (answer http://e-e.com/Q_26646667.html#a34237012 ) most of the credit!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34267820
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
 

Expert Comment

by:_alias99
ID: 34308452
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 34309218
Thanks & Good luck
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now