in reply to Re: find and replace counter
in thread find and replace counter

Global substitution along with 'e' to evaluate the replacement text is the trick although you'd be better off not using $& as it imposes a cost on all regular expessions in the program. See Special variables. And you left out the '<' and '>'.

Given that <xxx> is a constant string it isn't worth while capturing it. If the tag is to be passed in a function then something like

my $html = get('http://use.perl.org'); my $tag = '<xxx>'; # or '<' . shift . '>' or ... my $count = 0; $html =~ s/$tag/ '<' . ++$count . ">$tag" /ge;

The reference to LWP::Simple was inspired by this blog on Beautiful Code