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:
- A capturing group is used,
- $1 and \1 changed into $2 and \2,
- $1 . insert($2),
- The input pattern is corrected (I used \d where it should have been 'v')
- A space is added between \2 and value (it was missing)
|