For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > September 2006 > how to match the format time









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to match the format time
hhj

2006-09-23, 9:57 pm

hi guys!
i use this regular expression m/(^|\D)1?\d:[0-5]\d[ap][mM]/ig)to match
format time like12:00am 5:00pm 8:30AM, and 3:60am 99:00am 3:0pm is not
fit! it doesn't work, did anyone give a help.
m/1?\d:[0-5]\d[ap][mM]/ig can't match the data at the begin of line

hc.d2rk@gmail.com

2006-09-24, 7:57 am

Probably:
m{(\d+\:\d+[AaPp][mM])}igsm
?

Mumia W. (reading news)

2006-09-24, 7:57 am

On 09/23/2006 10:16 PM, hhj wrote:
> hi guys!


Hi

> i use this regular expression m/(^|\D)1?\d:[0-5]\d[ap][mM]/ig)to match
> format time like12:00am 5:00pm 8:30AM, and 3:60am 99:00am 3:0pm is not
> fit! it doesn't work, did anyone give a help.
> m/1?\d:[0-5]\d[ap][mM]/ig can't match the data at the begin of line
>


What about this?

use strict;
use warnings;

my @strs = ('12:00am 5:00pm 8:30AM','3:60am 99:00am 3:0pm');
foreach my $str (@strs) {
my @times = $str =~ /[0-9:]+[ap]m/ig;
print join(' / ',@times), "\n";
}


--
paduille.4058.mumia.w@earthlink.net
Paul Lalli

2006-09-25, 6:57 pm

hhj wrote:
> hi guys!
> i use this regular expression m/(^|\D)1?\d:[0-5]\d[ap][mM]/ig)to match
> format time like12:00am 5:00pm 8:30AM, and 3:60am 99:00am 3:0pm is not
> fit! it doesn't work, did anyone give a help.
> m/1?\d:[0-5]\d[ap][mM]/ig can't match the data at the begin of line


That wheel has already been invented:
http://search.cpan.org/~roode/Regex...me-0.01/time.pm

#!/usr/bin/perl
use strict;
use warnings;
use Regexp::Common qw/time/;

for my $time ('12:00am', '5:00pm', '8:30AM', '3:60am', '99:00am',
'3:0pm') {
print "$time ";
print $time =~ /^$RE{time}{hms}$/ ? "matches" : "doesn't match";
print "\n";
}
__END__

Output:
12:00am matches
5:00pm matches
8:30AM matches
3:60am doesn't match
99:00am doesn't match
3:0pm doesn't match

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com