Monday, 15 March 2010

regex - Reg Ex matching time range -



regex - Reg Ex matching time range -

this question has reply here:

regular look date range 1 reply

i have log file saves logs time stamp. have read values between time range. illustration origin of file , 5:3:55. not regex wondering how can write look read it. far have come next look not right. samples how write properly?

([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)

the post has solution range doesnt work needs taking range of time , dealing files. while need go start of file time stamp increment word matches.

you need bit more regex (at to the lowest degree create sane solution). here sample programme demonstrates simple solution in perl:

use warnings; utilize strict; $time_min = '08:19:48'; $time_max = '12:30:48'; while (1) { print "enter time: "; chomp($_ = <stdin>); if (time_numeric($_) <= time_numeric($time_max) , time_numeric($_) >= time_numeric($time_min)) { print "$_ between $time_min , $time_max\n"; } else { print "$_ ain't between $time_min , $time_max\n"; } } sub time_numeric { if ($_[0] =~ /^(\d{1,2}):(\d{1,2}):(\d{1,2})$/) { homecoming ($3 + $2 * 100 + $1 * 10000); } else { die "invalid time!"; } }

it doesn't bother validate time, assuming input real times. works 24 hr time stamps.

it work input whether or not has leading zeros in fields.

if working in perl, consider using module such time::piece, as demonstrated in answer.

regex expression

No comments:

Post a Comment