473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Regular expression help for 12 hour clock?

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
8 3416
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
2
by: Tedmond | last post by:
Dear all, I want to read a file data block by block using regular expression. The file contents is like MWH ........ ................. ..................... MWH ....................
6
by: Mahendra Kutare | last post by:
Or do we need to have any specific library ? I am more interested in knowing if we can do string pattern matching regular expression processing with C. Thanks Mahendra
1
Plater
by: Plater | last post by:
I'm having no end of trouble getting a correct regular expression for what I am doing. It probably would have taken me 5mins to produce a plain text matching system with string indexes, but even...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.