in reply to Re: A text replacement question
in thread A text replacement question

(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:

Replies are listed 'Best First'.
Re^3: A text replacement question
by Anonymous Monk on Feb 15, 2005 at 18:22 UTC
    hmm i get this error; Variable length lookbehind not implemented in regex; marked by <-- HERE in m/(?<=weak<INPUT TYPE=radio NAME=(\w{2}\d\w{2}) VALUE +=\d>(?:<INPUT TYPE=radio NAME=\1VALUE=\d>){6})(?=strong) <-- HERE / at -e line 2. what does this mean? Thanks for the help! Great stuff. (steamerboy)