#!/usr/bin/perl while( ) { chomp; my( $year, $julian, $hour, $minute, $second ) = m/ (\d{4}) # year (?: # group the day - time portions -? # optional hyphen (\d{1,3}) # julian day (?: # group the time portions \s+ # one or more whitespace (\d\d) # hour : (\d\d) # minute : (\d\d) # second )? # time is optional )? # day - time is optional /xg; print <<"HERE"; For input [$_] Year: $year Julian: $julian Hour: $hour Minute: $minute Second: $second HERE } __END__ 2005-100 10:12:01 2005100 10:12:01 2005-100 2005100 2005