c:\@Work\Perl\monks>perl -wMstrict -le "my $timeLimit = 'xxx endTime=\"2016-12-28T23:59:59\" startTime=\"2016-09-30T00:00:00\" yyy'; ;; $timeLimit =~ m/startTime=\"(.*?)\"/g; my $startTime = $1; print 'match position after 1st match: ', pos $timeLimit; ;; my $endTime; if ($timeLimit =~ m/endTime/) { $timeLimit =~ m/endTime=\"(.*?)\"/g; $endTime = $1; print 'match position after 2nd match: ', pos $timeLimit; } print \"[$startTime],[$endTime]\n\"; " match position after 1st match: 65 Use of uninitialized value in print at -e line 1. match position after 2nd match: Use of uninitialized value $endTime in concatenation (.) or string at -e line 1. [2016-09-30T00:00:00],[] #### c:\@Work\Perl\monks>perl -wMstrict -le "my $timeLimit = 'xxx endTime=\"2016-12-28T23:59:59\" startTime=\"2016-09-30T00:00:00\" yyy'; ;; my $got_start = my ($start) = $timeLimit =~ m/startTime=\"(.*?)\"/; my $got_end = my ($end) = $timeLimit =~ m/endTime=\"(.*?)\"/; ;; print qq{start [$start], end [$end]} if $got_start and $got_end; " start [2016-09-30T00:00:00], end [2016-12-28T23:59:59]