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],[]