Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 4th, 2009, 09:21 AM
 shleda
Born Looser

Join Date: Mar 2002
Location: /root
Posts: 272
Time spent in forums: 23 h 14 m 11 sec
Reputation Power: 172
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);

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 09:22 AM. Reason: TyPo5 :P

#2
February 4th, 2009, 10:42 AM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
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.

- "{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 10:45 AM.

#3
February 4th, 2009, 11:02 AM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
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```

#4
February 5th, 2009, 04:40 AM
 shleda
Born Looser

Join Date: Mar 2002
Location: /root
Posts: 272
Time spent in forums: 23 h 14 m 11 sec
Reputation Power: 172
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?

#5
February 5th, 2009, 04:47 AM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
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!

 Viewing: Dev Shed Forums > Programming Languages - More > Regex Programming > Check if the input is either -1 or 1 to 32767