Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2009, 08:21 AM
shleda's Avatar
shleda shleda is offline
Born Looser
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: /root
Posts: 272 shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 23 h 14 m 11 sec
Reputation Power: 172
Send a message via MSN to shleda Send a message via Yahoo to shleda Send a message via Google Talk to shleda Send a message via Skype to shleda
Facebook
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

Reply With Quote
  #2  
Old February 4th, 2009, 09:42 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
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.

Reply With Quote
  #3  
Old February 4th, 2009, 10:02 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
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

Reply With Quote
  #4  
Old February 5th, 2009, 03:40 AM
shleda's Avatar
shleda shleda is offline
Born Looser
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: /root
Posts: 272 shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level)shleda User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 23 h 14 m 11 sec
Reputation Power: 172
Send a message via MSN to shleda Send a message via Yahoo to shleda Send a message via Google Talk to shleda Send a message via Skype to shleda
Facebook
Thumbs up 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?

Reply With Quote
  #5  
Old February 5th, 2009, 03:47 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Check if the input is either -1 or 1 to 32767

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap