Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2005, 1:11 PM   #1
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Regular Exp help

Yo all! ^_^

I'm trying to make a reg exp that will match a users inputed time. I want the RE to match a string like 8:00am or 12:00pm. My version of the RE is as follows:

var regEx = new RegExp ("(1-12):(0)*(0-60)(am|pm)")

But...of course...it dosen't work. It ALWAYS returns false. (I'm not great with RE... ) Can I get some help? And just to point out, I want the test to fail if it is not in the form (x)x:xx(am|pm).

Thanks a lot! ^_^
MegaArcon is offline   Reply With Quote
Old Dec 7th, 2005, 1:18 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You can only use hyphens to say "everything between these two" in character sets ([ ]):
(0?[1-9]|1[0-2]):([0-5][0-9])(am|pm)
That's how I would do it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 7th, 2005, 1:50 PM   #3
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Say, that's pretty cool. ^_^ The only problem that it runs into is that it accepts a time like 92341:00pm. Interestingly enough, it dosen't except 1:324324pm. I don't see any real difference in the re that casues the first part to not act as expected but the second part to be fine....strange...

Last edited by MegaArcon; Dec 7th, 2005 at 2:15 PM.
MegaArcon is offline   Reply With Quote
Old Dec 7th, 2005, 3:29 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Yo all! ^_^ You need to put some restrictions on the number of things that can match before the parser moves on and looks farther, dontchano.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 7th, 2005, 4:17 PM   #5
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Just add the beginning/end symbols on:
^(0?[1-9]|1[0-2]):([0-5][0-9])(am|pm)$
Should do it.

--47.
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Dec 8th, 2005, 7:14 AM   #6
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Yup, that did the trick Agent. Thanks all! ^_^ This forum rules all!
MegaArcon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC