Solved

Regular Expression for comma range

Posted on 2010-11-30
20
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 75

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

756 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