tags:

views:

75

answers:

3

All i want to do is match a number between 2-16, spanning 1 digit to 2 digits

according to http://www.regular-expressions.info/numericranges.html, they have examples for 1 or 2 digit ranges, but not something that spans both..if that makes sense.

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99.

Something like "^[2-9][1-6]$" matches 21 or even 96 lol. Any help would be appreciated.

+7  A: 
^([2-9]|1[0-6])$

will match either a single digit between 2 and 9 inclusive, or a 1 followed by a digit between 0 and 6, inclusive.

cHao
Simple and to the point. +1.
Joey
+1 and deleting my answer because you beat me by 8 seconds.
Billy ONeal
+2  A: 

With delimiters (out of habit): /^([2-9]|1[0-6])$/

The regex itself is just: ^([2-9]|1[0-6])$

eldarerathis
The slashes are PHP specific and probably should be removed when someone's talking about a general regular expression.
Billy ONeal
@Billy ONeal: That's not at all PHP specific. It can be used in any language that lets you pick your delimiter.
eldarerathis
Perl had them before PHP. In any case, they're not part of the expression, indeed. If your language requires you to use something like that then the person who asked can trivially add them.
Joey
@Billy many languages specify regex *literals* using slashes.
Daniel Vandersluis
Not all languages need delimiters -- they can be passed a regex as a normal string.
cHao
@eldareathis: Okay, perhaps it exists in some other languages, but the fact of the matter is that they are not part of the regex. Given that the OP has not specifically identified the flavor he is using they should be omitted.
Billy ONeal
@Billy ONeal: Which I went back and edited in. I didn't disagree with that, which is why I added the edit. That doesn't make my assertion in my previous comment any less correct, though.
eldarerathis
@eldarerathis: +1 for edit.
Billy ONeal
@Billy ONeal: Thank you. And I apologize if my previous statement came off as confrontational. I do understand your point, and I'm going to try to notice when I post regexes like that in the future. Using `/` has just gotten ingrained into my head at this point...
eldarerathis
A: 
^([2-9]|1[0-6])$

(Edit: Removed quotes for clarification.)

Alek
The quotes are specific to how the regex would be inserted into a programming environment, but should probably be removed when talking about regexes in the abstract.
Billy ONeal
Thanks; corrected.
Alek
Your regex "^[2-9]|1[0-6]$" is wrong, because the "|" separates the "^" to the first pattern and the "$" to the second one. You need parentheses here, e.g.: "^([2-9]|1[0-6])$"
vog
Thanks again. I always check my answers on some tool, but in this case I was fooled by a simple test in Python.
Alek