The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Check if the input is either -1 or 1 to 32767
Discuss Check if the input is either -1 or 1 to 32767 in the Regex Programming forum on Dev Shed. Check if the input is either -1 or 1 to 32767 Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2009, 08:21 AM
|
 |
Born Looser
|
|
Join Date: Mar 2002
Location: /root
|
|
|
Check if the input is either -1 or 1 to 32767
I am trying to create an expression that checks if the input number is
a) either -1
b) either in the range of 1-32767
c) nothing else than specified above
I have following expression which tests true for anything greater than 32767 but fails for 0 or -1.
Code:
var expr = /^([1-2]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}|3[0-1]{1}[0-9]{1}[0-9]{1}[0-9]{1}|32[0-6]{1}[0-9]{1}[0-9]{1}|327[0-5]{1}[0-9]{1}|3276[0-7]{1})$/;
var myresult = expr.test(0);
alert(myresult);
Any pointer...???
__________________
Kumar Chetan Sharma
-----
_SelfProcclaimedGuru
Hire a LAMP guy.
To err is human. To blame your computer for your mistakes is even more human, it is downright natural.
Last edited by shleda : February 4th, 2009 at 08:22 AM.
Reason: TyPo5 :P
|

February 4th, 2009, 09:42 AM
|
|
|
Quote: | Originally Posted by shleda I am trying to create an expression that checks if the input number is
a) either -1
b) either in the range of 1-32767
c) nothing else than specified above
I have following expression which tests true for anything greater than 32767 but fails for 0 or -1.
Code:
var expr = /^([1-2]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}|3[0-1]{1}[0-9]{1}[0-9]{1}[0-9]{1}|32[0-6]{1}[0-9]{1}[0-9]{1}|327[0-5]{1}[0-9]{1}|3276[0-7]{1})$/;
var myresult = expr.test(0);
alert(myresult);
Any pointer...??? |
First, I must say that validating numerical values with regex, is not the preferred way to go.
Having said that, a couple of remarks about your current approach:
- "{1}" can be removed, they only clutter up your regex
- "{0,1}" can be replaced by "?"
- this: "[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}" can be replaced by: "[0-9]{0,4}"
- "[0-9]" can be replaced by "\d"
- a range like "[1-2]" (two successive numbers) can be replaced by "[12]"
If you apply all those things (and a bit more to reject "0" and accept "-1"), you get this:
Code:
^(-1|[12]?[1-9]\d{0,3}|3[01]{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])$
Good luck!
Last edited by prometheuzz : February 4th, 2009 at 09:45 AM.
|

February 4th, 2009, 10:02 AM
|
|
|
This might be a bit more intuitive:
Code:
// regex:
^(-1|[1-9]\d{0,3}|[12]\d{4}|3[01]\d{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])$
// explanation:
^ // start of the string
( // start group 1
-1 // matches -1
| [1-9]\d{0,3} // or matches 1-9999
| [12]\d{4} // or matches 10000-29999
| 3[01]\d{3} // or matches 30000-31999
| 32[0-6]\d{2} // or matches 32000-32699
| 327[0-5]\d // or matches 32700-32599
| 3276[0-7] // or matches 32760-32767
) // end group 1
$ // end of the string
|

February 5th, 2009, 03:40 AM
|
 |
Born Looser
|
|
Join Date: Mar 2002
Location: /root
|
|
Whoa!!!
Quote: | Originally Posted by prometheuzz This might be a bit more intuitive:
Code:
// regex:
^(-1|[1-9]\d{0,3}|[12]\d{4}|3[01]\d{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])$
// explanation:
^ // start of the string
( // start group 1
-1 // matches -1
| [1-9]\d{0,3} // or matches 1-9999
| [12]\d{4} // or matches 10000-29999
| 3[01]\d{3} // or matches 30000-31999
| 32[0-6]\d{2} // or matches 32000-32699
| 327[0-5]\d // or matches 32700-32599
| 3276[0-7] // or matches 32760-32767
) // end group 1
$ // end of the string
|
It is really intuitive and added some thing to my knowledge. How do I rate your post?
|

February 5th, 2009, 03:47 AM
|
|
|
Quote: | Originally Posted by shleda It is really intuitive and added some thing to my knowledge. How do I rate your post? |
There's a scale on the upper right corner of each post where you can indicate how helpful (or unhelpful) a post is.
Good to hear my post was helpful to you!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|