# Not greedy pos() = 0; $string = q(xxxx); $count = ($string =~ s/[^x]*?/#/g); print qq($string $count), $/; __OUTPUT__ #x#x#x#x# 5