•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 397,591 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,863 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 2936 | Replies: 2
![]() |
•
•
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation:
Rep Power: 4
Solved Threads: 19
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.
[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.
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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
Similar Threads
- Parsing a log file using regular expressions (C#)
- Regular Expressions (VB.NET)
- Regular Expressions (Computer Science and Software Design)
- Regular Exp (Python)
- mod_rewrite: help with regular expressions (Linux Servers and Apache)
- matching regular expressions (Java)
Other Threads in the C# Forum
- Previous Thread: Inharits userControl
- Next Thread: Need Email validation source code (C#)


Linear Mode