how to match the format time
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
Re: how to match the format time
Probably:
m{(\d+\:\d+[AaPp][mM])}igsm
?
Re: how to match the format time
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 [at] strs = ('12:00am 5:00pm 8:30AM','3:60am 99:00am 3:0pm');
foreach my $str ( [at] strs) {
my [at] times = $str =~ /[0-9:]+[ap]m/ig;
print join(' / ', [at] times), "\n";
}
--
paduille.4058.mumia.w [at] earthlink.net
Re: how to match the format time
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/Regexp-Common-time-0.01/time.p m
#!/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