943,745 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5765
  • C# RSS
Jul 26th, 2005
0

Regular Expressions

Expand Post »
ok, new to regular expressions so can someone please explain something to me. In trying to create an expression to check if an entry is between 1 and 15, I first came up with:
[1-9]|1[0-5]
this didn't work however
1[0-5]?|[2-9]
did work.

I understand why the second one works, but not why the first one didn't.
Similar Threads
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jul 26th, 2005
0

Re: Regular Expressions

k... I'm not a RexEx guru either but I'm going to give it a shot, bolded blue text text will be subject of my explanation when regex expressions are used...

your expression [1-9]|1[0-5]

Scenario A: [1-9]|1[0-5] means match any number 1 thru 9, pretty basic
[1-9]|1[0-5] is a logical operator [OR]
Scenario B:[1-9]|1[0-5] means any number zero thru five

ok now that we broke it down lets look at what your expression would match...
1 = match based on scenario A
2 = match basesed on A
... up to 9 would match!
10 = now comes the fun part the 1 would match Scenario A and since there was a match it doesnt evaluate it under scenario B and 0 gets ignored.
11 = again Scenario A twice and doesn't even get to scenario B

so for it two work you must have it backwards
1[0-5]|[1-9]

and I would even go a step further and make sure the match is not in the middle of a string by adding ^ in the begging of the expression to indicate begining of line or a word and $ at the end of the expression to indicated end of line or word, like so...
(^1[0-5]$)|(^[1-9]$)

I hope this helps.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
senexom is offline Offline
54 posts
since Jun 2005
Jul 27th, 2005
0

Re: Regular Expressions

cool thanks
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Inharits userControl
Next Thread in C# Forum Timeline: Help with Icons





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC