my $t = "This example could be another example where the example is the example I wanted\n"; my $count = 0; while($t =~ /example\s/){ my $example = "example_".++$count; $t =~ s/(example\s)/$example /; print $t; } print $t;