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)
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. :)
> 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;
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, ...
> > 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;
> 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)
> 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.
> 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;
> 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)