PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 20, 2020 7:36 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Time
PostPosted: Wed Dec 21, 2011 11:40 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
 Post subject: Re: Time
PostPosted: Wed Dec 21, 2011 2:25 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
uhm, how about Date::createFromFormat() ?


Top
 Profile  
 
 Post subject: Re: Time
PostPosted: Wed Dec 21, 2011 2:40 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Hi Celauran!

Looks like you've written a nice long expression... Cool!
Let's see if we can tweak it as you were asking.

First thing that jumps at me: you have four places with a 1 in square brackets: {1}
You can remove those. Any time you have a character class, it already means "match one of these", unless you have a quantifier.

Second tiny optimization: you can take the "M" out of (AM|PM), yielding something like:
Syntax: [ Download ] [ Hide ]
(?:(?:A|P)M)


Third thought: On the line above, you can see some non-capturing groups. Your expression has four capturing groups. If you don't plan to capture anything, you can get rid of that overhead by using non-capturing groups. Or, in the case of the lone space character (\s), forget the group altogether.

One more idea: some places use different dividers for the hours and minutes, or none at all. For instance, 1855, or 12.44. So you could add characters in that class, or make it optional.

Also: no need to insert a single digit in a character class, as in the two instances of [\d]

If we make all these tweaks, here's where we are so far (without the delimiters):

Syntax: [ Download ] [ Hide ]
(?i)^(?:[01]?\d|2[0-3])[.:h]?[0-5]\d\s?(?:(?:A|P)M)?


But oops, if you have no AM or PM, this will capture a space character after the time! So you need to make the space character part of the AM PM option. Let's fix this:

Syntax: [ Download ] [ Hide ]
(?i)^(?:[01]?\d|2[0-3])[.:h]?[0-5]\d\(?:s?(?:(?:A|P)M))?


But wait a minute... As it is, the expression allows 08:55 pm and 22:22am. You need to decide if you want to validate the date as well or not. If so, you'll have to capture the hour and control the am pm with conditionals. And we can no longer break the hour at 00-19 vs 20-24. Instead, we need to test for 00 to 12 vs everything else. (If you think 12:33pm is legit). This is where it leads us:

Syntax: [ Download ] [ Hide ]
(?i)^(?:([0]?\d|1[012])|(?:1[3-9]|2[0-3]))[.:h]?[0-5]\d(?:\s?(?(1)(?:(?:A|P)M)))?


There might still be a bug or two in there, but without spending an hour on it, that's how I would start debugging your expression.

Hope this is the kind of info you were looking for!
:)

Wishing you a fun day.


Top
 Profile  
 
 Post subject: Re: Time
PostPosted: Wed Dec 21, 2011 3:00 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
One more thought:
At the moment, Am and pM are legal. It's up to you whether to accept that. If not, we need to remove the case insensitive matching and specify the allowed strings: am|AM|pm|PM

Also:
Checking Jeffrey's book, I see that Mastering Regular Expressions has this (after correcting a typo):
Syntax: [ Download ] [ Hide ]
(1[012]|[1-9]):[0-5][0-9] (am|pm)

It doesn't seem to go as far as what we've done. But maybe that's a logic to use for the am/pm test in our expression (captured in group 1), as I haven't fully thought through / debugged that part (what is a legal time to use with the am / pm attributes).
I looked through Jan's Regular Expressions Cookbook and didn't see a time pattern.


Top
 Profile  
 
 Post subject: Re: Time
PostPosted: Thu Dec 22, 2011 9:14 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group