RexFlex23

Have a .NET application that allows people to specify pages in a free-for textbox. Such as:

1,2,5-8,10-20,22,23

Am hoping that I can use Regular Expressions to help rather than having to write custom code to check.

1. Only Digits, comma, and dash(minus) are valid

2. If comma is found, digit must be on both sides of the comma

3. If dash is found, digit must be on both sides of the dash

4. Spaces are not allowed

5. 0 (zero) is not allowed

Seems pretty straight forward, however I am struggling big time getting something to work. This is my first attempt at using a Regular Expression. I've been looking at examples, but can't seem to translate them into something I can use. If anyone has any ideas or examples it would be much appreciated!

Thanks in advance!




Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

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

will validate

1,2,5-8,10-20,22,23

unfortunately it's much tougher to invalidate input like

1,1.2-2,2-4,4-2

which i guess is a possibility





Re: Regular Expressions HELP... Need to validate a list of page numbers...

RexFlex23

Thank You!!! Why not validate what it should be vs what could be wrong with it.... makes total sense... The expression you provided works like I wanted it to!! Thanks




Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

u don't follow. I meant to say that b/c of the logic constraints of the pattern, the regex will validate also input like

1,1,1,1,1,1,1,1,1,111-34





Re: Regular Expressions HELP... Need to validate a list of page numbers...

RexFlex23

I have code in place that won't allow a "range" to be backwards, meaning 111-34 is not valid, and the 1,1,1,1,1,1,1... although valid wouldn't be applied given how I'm using my list of objects and populating things before hitting the database. I'm also checking to see if any of the numbers are greater than the total number of pages currently available, if so it's invalid.

Then again I could still be missing something...as I was looking at the initial problem differently than your solution..






Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

it's good if u r able to invalidate 1) backwards ranges and also 2) repeated ranges like

1-2,1-2

b/c regex cannot do 1) though 2) is in principle possible

if so then the last 2 things on regex side we need to take care of is to invalidate inputs like

1-2,0-2,0-5

and 1-2,1-2

i'll post more





Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

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

will invalidate

1,2,5-8,0-20,22,23

if this is in your spec

>>>>>>>>>>>>>>>>>>>>>>>>

Q for u: how about cases like

1-4, 2-5

regex cannot really invalidate them





Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

2) repeated ranges like

1-2,1-2

are tougher to deal with than i thought..I'll get back to it if i find time





Re: Regular Expressions HELP... Need to validate a list of page numbers...

RexFlex23

I've modified the expression you provided because they can start out with a range or a comma separated list:

^( !0\d*)\d+((,( !0)\d+)|(-( !0)\d+) )*$

I currently have code that will handle the duplicate entries. Before going to the database I rip apart the ranges anyways and put in the actual numbers so 1-5, would be 1,2,3,4,5. Then I populate a list and check for existence before adding again.

If you can come up with a solution to being able to check duplicate entries that'd be great, but as it currently sits it's working as I would want when I include my .NET code.

I appreciate the help!!

Thanks






Re: Regular Expressions HELP... Need to validate a list of page numbers...

Sergei Z

u probably did not realize that when u made amendments to the regex i sent, but your modified version:

^( !0\d*)\d+((,( !0)\d+)|(-( !0)\d+) )*$

will match on input like

1-2-2

is it OK if not, what did u try to accomplish enhancing the regex