Google Groups Home
Help | Sign in
Regular expression help for 12 hour clock?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
mw  
View profile
 More options Jul 6, 2:11 am
Newsgroups: comp.lang.javascript
From: mw <mwolff...@gmail.com>
Date: Sat, 5 Jul 2008 23:11:54 -0700 (PDT)
Local: Sun, Jul 6 2008 2:11 am
Subject: Regular expression help for 12 hour clock?
Hello,

I am trying to make a regex for a 12 hour clock, and I can't seem to
get it just right.

What I have now is

  var regexmatch = /(1[012]:[0-5][0-9]|[1-9]:[0-5][0-9])\s((a\.m\.)|(p
\.m\.))/i

My thinking is that the first part will filter out the time and the |
will correctly check the two possibilities

1:00 --> 9:59
and then the other part
10:00-->12:59

then a space which is \s

then the tricky part where I'm pretty sure the bug is: the a.m. or
p.m.  I think it is my inexperience with the () that is messing this
up.  I've tried all sorts of variations for the last part, but I am
stuck.  I keep ending up with expressions that are too liberal and
will allow times like 23:23 p.m. through.  Please excuse my slow-
learning of the subject as tonight is the first time I've touched
regex in js and really regex at all.  Also, I'm sorry for the a.m.
p.m. part.  It has gone from OK looking to ugly parenthesis nightmare
in my attempts to make it work correctly.  (I've tried other without
() versions, etc, but can't seem to hit it correctly)

Thanks in advance,
mw


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lasse Reichstein Nielsen  
View profile
(1 user)  More options Jul 6, 5:43 am
Newsgroups: comp.lang.javascript
From: Lasse Reichstein Nielsen <l...@hotpop.com>
Date: Sun, 06 Jul 2008 11:43:00 +0200
Subject: Re: Regular expression help for 12 hour clock?

mw <mwolff...@gmail.com> writes:
> I am trying to make a regex for a 12 hour clock, and I can't seem to
> get it just right.

> What I have now is

>   var regexmatch = /(1[012]:[0-5][0-9]|[1-9]:[0-5][0-9])\s((a\.m\.)|(p
> \.m\.))/i

Seems about right. You can make it a little more compact by not
repeating the pattern for minutes, and you have capture groups for
both a.m., p.m. and the alternative. You might want to use a
non-capturing parentheses for one of them (or all, if you don't use
the captured groups at all).

The actual problem is that you haven't anchored the expression, so
it can match a sub-string of the string you use it on. I.e.,
it matches "23:13 p.m." because it matches the substring "3:13 p.m.".

I.e., something like /^((?:1[012]|[1-9]):[0-5][0-9])\s([ap]\.m\.)$/i.

If you do want to match substrings of longer strings, then you can't
use "^" and "$" as anchors. Instead you can start with a "\b" assertion
(i.e., the character before the first digit was not a word character):

 /\b((?:1[012]|[1-9]):[0-5][0-9])\s([ap]\.m\.)/ig

Or, if you only want to check the format, not try to parse it,
you can do away with capturing parentheses altogether.
For total compactness and (my preferences for) readabiliy:
 /^(?:[1-9]|1[0-2]):[0-5]\d\s[ap]\.m\.$/i

> then the tricky part where I'm pretty sure the bug is: the a.m. or
> p.m.

Don't think so. It seems to do what you want it to ...

>  I think it is my inexperience with the () that is messing this
> up.  I've tried all sorts of variations for the last part, but I am
> stuck.  

... byut you do have an awful lot of parentheses. Only have capturing
groups "("..")" when you actually want to extract the submatch later,
and use non-capturing ones "(?:"..")" when you just want to group
parts of the expression (like parentheses in mathematics).

> I keep ending up with expressions that are too liberal and
> will allow times like 23:23 p.m. through.  

That's due to not anchoring. You correctly match "3:23 p.m." in that
string.

> Please excuse my slow- learning of the subject as tonight is the
> first time I've touched regex in js and really regex at all.

Then you are doing really well. Trust me, I've seen lots worse. :)

> Also, I'm sorry for the a.m.
> p.m. part.  It has gone from OK looking to ugly parenthesis nightmare
> in my attempts to make it work correctly.  (I've tried other without
> () versions, etc, but can't seem to hit it correctly)

Maye it's because you are trying to fix a problem that is really in
another part of the expression. :)

/L
--
Lasse Reichstein Nielsen
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SAM  
View profile
(1 user)  More options Jul 6, 6:25 am
Newsgroups: comp.lang.javascript
From: SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
Date: Sun, 06 Jul 2008 12:25:52 +0200
Local: Sun, Jul 6 2008 6:25 am
Subject: Re: Regular expression help for 12 hour clock?
mw a écrit :

> Hello,

> I am trying to make a regex for a 12 hour clock,

is that to say :
   12.00 a.m. -> 12.59 a.m. then 1:00 a.m. -> 11:59 a.m.
then
   12.00 p.m. -> 12.59 p.m. then 1:00 p.m. -> 11:59 p.m.
??

I do not understand this kind of notation can be yet used when so much
parts of the world doesn't use exactly the same ... (noon / midnight)
<http://en.wikipedia.org/wiki/12-hour_clock>

> and I can't seem to get it just right.

> What I have now is

>   var regexmatch = /(1[012]:[0-5][0-9]|[1-9]:[0-5][0-9])\s((a\.m\.)|(p
> \.m\.))/i

var regexmatch = /(^1[0-2]|^[1-9]):[0-5][0-9]\s[ap]\.m\./i;

--
sm


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile
(1 user)  More options Jul 6, 7:46 am
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <J.R.Stock...@physics.org>
Date: Sun, 6 Jul 2008 04:46:57 -0700 (PDT)
Local: Sun, Jul 6 2008 7:46 am
Subject: Re: Regular expression help for 12 hour clock?
On Jul 6, 11:25 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:

> mw a écrit :
> > I am trying to make a regex for a 12 hour clock,
> I do not understand this kind of notation can be yet used when so much
> parts of the world doesn't use exactly the same ... (noon / midnight)
> <http://en.wikipedia.org/wiki/12-hour_clock>

Well, it could be classwork; it's a valid exercise, even here in
Europe.

But mw is probably an American - no more need then be said.

--
  (c) John Stockton, near London, UK.  Posting with Google.
 Mail: J.R.""""""""@physics.org or (better) via Home Page at
 Web:  <URL:http://www.merlyn.demon.co.uk/>
 FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mw  
View profile
 More options Jul 6, 12:14 pm
Newsgroups: comp.lang.javascript
From: mw <mwolff...@gmail.com>
Date: Sun, 6 Jul 2008 09:14:19 -0700 (PDT)
Local: Sun, Jul 6 2008 12:14 pm
Subject: Re: Regular expression help for 12 hour clock?
On Jul 6, 5:25 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:

Hey, thanks everyone for their posts!  I used a bit from both and
rated both highly in google groups. (for whatever that is worth)
It seems to be working quite well for me now!  The final code I used
is a bit of a regex from my post, and both of the above posts.
The code I'm using now(which has some ()'s still because I decided I
may actually use them later for remembering the text) is

  var regexmatch = /(^1[012]:[0-5][0-9]|^[1-9]:[0-5][0-9])\s[ap]\.m\./
i;

Anyway,
Thanks again!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile
 More options Jul 6, 12:42 pm
Newsgroups: comp.lang.javascript
From: "Evertjan." <exjxw.hannivo...@interxnl.net>
Date: 06 Jul 2008 16:42:39 GMT
Local: Sun, Jul 6 2008 12:42 pm
Subject: Re: Regular expression help for 12 hour clock?
mw wrote on 06 jul 2008 in comp.lang.javascript:

> Hey, thanks everyone for their posts!  I used a bit from both and
> rated both highly in google groups. (for whatever that is worth)
> It seems to be working quite well for me now!  The final code I used
> is a bit of a regex from my post, and both of the above posts.
> The code I'm using now(which has some ()'s still because I decided I
> may actually use them later for remembering the text) is

>   var regexmatch = /(^1[012]:[0-5][0-9]|^[1-9]:[0-5][0-9])\s[ap]\.m\./
> i;

 var regexmatch = /^(1[012]|[1-9]):[0-5]\d\s[ap]\.m\.$/i;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr J R Stockton  
View profile
 More options Jul 6, 2:13 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <j...@merlyn.demon.co.uk>
Date: Sun, 6 Jul 2008 19:13:58 +0100
Local: Sun, Jul 6 2008 2:13 pm
Subject: Re: Regular expression help for 12 hour clock?
In comp.lang.javascript message <a51d38b9-7134-49df-8466-c16c86d92600@34
g2000hsh.googlegroups.com>, Sun, 6 Jul 2008 09:14:19, mw
<mwolff...@gmail.com> posted:

>  var regexmatch = /(^1[012]:[0-5][0-9]|^[1-9]:[0-5][0-9])\s[ap]\.m\./
>i;

That should serve if it is a piece of coursework.  But it is rather
rigorous.  It would reject
        04:44 a.m.
        11:22 pm
        3:36  a.m.
         1:33 a.m.

Given a choice, I'd be tempted to try
        /^\s*(\d+):(\d+)\s+([ap])(\.?)m\4\s*$/i
and to check the first two matches arithmetically.  Except that I'd use
the 24-hour clock, and maybe \d\d? for \d+ .

It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.

--
 (c) John Stockton, nr London UK.   ?...@merlyn.demon.co.uk     IE7 FF2 Op9 Sf3
 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SAM  
View profile
 More options Jul 6, 8:39 pm
Newsgroups: comp.lang.javascript
From: SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
Date: Mon, 07 Jul 2008 02:39:10 +0200
Local: Sun, Jul 6 2008 8:39 pm
Subject: Re: Regular expression help for 12 hour clock?
Evertjan. a écrit :

> mw wrote on 06 jul 2008 in comp.lang.javascript:

>> Hey, thanks everyone for their posts!  I used a bit from both and
>> rated both highly in google groups. (for whatever that is worth)
>> It seems to be working quite well for me now!  The final code I used
>> is a bit of a regex from my post, and both of the above posts.
>> The code I'm using now(which has some ()'s still because I decided I
>> may actually use them later for remembering the text) is

>>   var regexmatch = /(^1[012]:[0-5][0-9]|^[1-9]:[0-5][0-9])\s[ap]\.m\./
>> i;

>  var regexmatch = /^(1[012]|[1-9]):[0-5]\d\s[ap]\.m\.$/i;

var regexmatch = /^(1[012]|[1-9]):[0-5]\d\s[ap]\.?m\.?$/i;

--
sm


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evertjan.  
View profile
 More options Jul 7, 6:54 am
Newsgroups: comp.lang.javascript
From: "Evertjan." <exjxw.hannivo...@interxnl.net>
Date: 07 Jul 2008 10:54:45 GMT
Local: Mon, Jul 7 2008 6:54 am
Subject: Re: Regular expression help for 12 hour clock?
SAM wrote on 07 jul 2008 in comp.lang.javascript:

The implied using of .match() can better be exchanged for .test(), so

var regextest = /^(1[012]|[1-9]):[0-5]\d\s[ap]\.?m\.?$/i;

The whole idea of using am/pm is wrong,
it leads to misakes outside strict local area's, so use:

var testResult = false;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google