Regular expression help for 12 hour clock?

M

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
 
L

Lasse Reichstein Nielsen

mw said:
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
 
S

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)
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;
 
M

mw

mw a écrit :
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)
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;

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!
 
E

Evertjan.

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;
 
D

Dr J R Stockton

In comp.lang.javascript message <a51d38b9-7134-49df-8466-c16c86d92600@34
g2000hsh.googlegroups.com>, Sun, 6 Jul 2008 09:14:19, mw
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.
 
S

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;
 
E

Evertjan.

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top