All that  foo foo foo stuff... That's a lot of foo-ing around for a simple substitution — and a lot of room for error to creep in. Another way (needs Perl version 5.10+ regex extensions):

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $s = qq{www<foo> 3 </foo>\n<bar>999</bar>\n<foo>14 </foo>xx\n} . qq{<bar>998</bar>\nyyy<foo> 159</foo>z\n<bar>997</bar>\n} ; print qq{[[$s]]}; ;; { my $n = 1; $s =~ s{ <(foo)> \s* \K \d+ (?= \s* </\g-1>) }{ $n++ }xmsge; } print qq{[[$s]]}; " [[www<foo> 3 </foo> <bar>999</bar> <foo>14 </foo>xx <bar>998</bar> yyy<foo> 159</foo>z <bar>997</bar> ]] [[www<foo> 1 </foo> <bar>999</bar> <foo>2 </foo>xx <bar>998</bar> yyy<foo> 3</foo>z <bar>997</bar> ]]
If you want to get rid of the leading/trailing whitespace, that's easily done. If you only have pre-5.10 regexes to work with, that can be handled too, although it's a bit more messy.


Give a man a fish:  <%-{-{-{-<


In reply to Re^5: sequential substitutions by AnomalousMonk
in thread sequential substitutions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.