(Update) code at the top of this post does NOT work

perl -pi.bak -e 's/(?<=weak<INPUT TYPE=radio NAME=(\w{2}\d\w{2}) VALUE +=\d>(?:<INPUT TYPE=radio NAME=\1VALUE=\d>){6})(?=strong)/insert($1)/e +ig; BEGIN { sub insert { my $name = shift; return "value 8, value 9, value + 10"; } }' *.html

Explenation of regex:

s/ (?<= # Look back-group weak # Match the word weak <INPUT TYPE=radio NAME= # tag ( # Capuring group: $1 / \1 \w{2}\d\w{2} # ) # End-capture VALUE=\d> # tag (?: # Non capturing group <INPUT TYPE=radio NAME= # tag \1 # It's possible that this should be $1, as I +said, the code is untested. VALUE=\d> ){6} # There are 7 input tags, one already matches +, so 6 to go. ) # End look-back (?= # Look-ahead-group strong ) # End look-ahead / the text you want to insert /ixg;

Big note, the code in the explenation will not work properly, because the x-modifier is in use and I did not escape the whitespace!

Update: after reading the reply of Anonymous Monk, I decided to test it... the variable lookbehind error comes from \1. This can be fixed by using (?=\1).{5} instead of \1, but then another errors shows up: 'Lookbehind longer than 255 not implemented'. This makes it impossible to use a look-behind...

So there is only one thing left to do and that is making it a 'real' group of it...

Then the short (and tested (or atleast on simple data)) version would become:
perl -pi.bak -e 's/(weak<INPUT TYPE=radio NAME=(\w{5}) VALUE=\d>(?:<INPUT TYPE=radio NAME=\2 VALUE=\d>){6})(?=strong)/$1 . insert($2)/eig; BEGIN { sub insert { my $name = shift; return "value 8, value 9, value 10"; } }' *.html

The differences:


In reply to Re^2: A text replacement question by Animator
in thread A text replacement question by steamerboy

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.