regex - Generic way to parse different date formats -


i have several different date formats parse, example:

$date1 = '^(\d\d),(\d\d) (\d\d):(\d\d):(\d\d)'; # dd,mm hh:mm:ss $date2 = '^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)'; # yyyy-mm-dd hh:mm:ss 

i want left variables

($year, $day, $month, $hour, $minute, $second) 

to populated correctly. can capture first date format in perl pattern like:

($day, $month, $hour, $minute, $second) = $log_line =~ /$date1/; $year = current_year; # guess date format has no year value 

but capture second date, need rearrange order of variables. don't want hardcode in ordering each type of date, know info has contained somewhere.

i want program scalable , maintainable; there simple solution program adapt different formats?

edit: realise ordering has stored somewhere, i'm looking concise option doing this.

you can solve immediate problem named capture groups:

if ( $str =~ /(?<year>\d\d\d\d)-(?<month>\d\d)-(?<day>\d\d)/ ) {   $year = $+{year};   $month = $+{month};   $day = $+{day}; } 

your more general problem best addressed various datetime::format modules cpan.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -