Count the match times
$text = "Name: Anne Name: A Name: C Name: D"; $match = 0; while ($text =~ /Name: *(\w+)/g) { ++$match; print "Match number $match is $1.\n"; }