By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Regular expression help for 12 hour clock?

P: n/a
mw
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
Jul 6 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
mw <mw*******@gmail.comwrites:
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.'
Jul 6 '08 #2

P: n/a
SAM
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
Jul 6 '08 #3

P: n/a
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, ...

Jul 6 '08 #4

P: n/a
mw
On Jul 6, 5:25*am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
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
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!
Jul 6 '08 #5

P: n/a
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)
Jul 6 '08 #6

P: n/a
In comp.lang.javascript message <a51d38b9-7134-49df-8466-c16c86d92600@34
g2000hsh.googlegroups.com>, Sun, 6 Jul 2008 09:14:19, mw
<mw*******@gmail.composted:
>
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 6 '08 #7

P: n/a
SAM
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
Jul 7 '08 #8

P: n/a
SAM wrote on 07 jul 2008 in comp.lang.javascript:
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;
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)
Jul 7 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.