use strict; use warnings; my $i = 0; while(){ s/.*?<\/foo>/"".++$i."<\/foo>"/e; print; } __DATA__ 3 a 14 bc 159 def