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 July 31st, 2011, 11:17 AM
SteveRoy SteveRoy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 2 SteveRoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 5 sec
Reputation Power: 0
How would you regex this "12345~12345~12345"?

I have a form input I'm trying to error check with JavaScript.
Only groups of 5 digits with a single '~' separating them.
For example:
Code:
 BAD 1
 BAD 12
 BAD 123
 BAD 1234
GOOD 12345
 BAD 123456
  OK 12345~
 BAD 12345~123
GOOD 12345~12345
 BAD 12345~12~12345
GOOD 12345~12345~12345

Using the Regex Powertoy the closest I came (I thought) was:
Code:
if(str.match(/\b[0-9]{5}\b/) && !str.match(/(\b[0-9]{0,4}\b)/)){

I need some Regex genius to explain a solution, if any.
Thanks

Reply With Quote
  #2  
Old July 31st, 2011, 01:02 PM
JClasen JClasen is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2007
Posts: 1,513 JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level)JClasen User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 7 h 10 m 22 sec
Reputation Power: 1422
I'd try something different, than the word boundary approach. I'm not entirely sure, if that works for JS, but please try, if this fits your needs:
Code:
/^((^|~)(\d{5}))+~?$/


The idea is to match five figures, which follow either the start of a given string or a ~. To make your "ok case" possible, the optional ~ before the end of the given string is appended to the pattern. Unfortunately, this would allow for a string like 12345~12345~ and you didn't say, what should happen in this case.

Regards, Jens

Reply With Quote
  #3  
Old July 31st, 2011, 01:31 PM
SteveRoy SteveRoy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 2 SteveRoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 5 sec
Reputation Power: 0
And makes all kinds of sense, and works perfectly.
It's logically obvious and I should have gotten it.
I never used such an obtuse conditional system before.
I guess, I just couldn't see the forest through the trees.

The "OK" case was just my way of saying that didn't mater if it passed or not. I suppose I'd prefer it didn't, so I pulled "~?".

Thanks a million.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > After 2 days, it still won't work.

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