in reply to extracting regex into, and interpolating, variable

I think the following aproach will achieve the string replacement you are looking for though you might want to refine it. I am assuming for example that there will always be a %bar% on a line with <textarea!
use strict; my $line = qq(<textarea type="text" value="foo" name="text_message" /> +&nbsp;<%bar%>&nbsp;</p>); my $foo = "foo"; if ($line =~ /<textarea .+<(%.+%)>/) { my $bar = $1; $line =~ s/<textarea/<input/; $line =~ s/$bar/$foo/; print $line; } exit;

Replies are listed 'Best First'.
Re: Re: extracting regex into, and interpolating, variable
by zusuki-san (Initiate) on Oct 23, 2002 at 14:41 UTC
    Hi, the initial post was mine, I posted anonymously by mistake.
    Thanks for the tip. The problem is that I can't guarantee the value of "foo" or indeed the attributes of "textarea", so I have to do some kind of pattern match like m/\(<textarea.*?>\)/ and then do a match on that to get the value of "value", if that makes sense.
    then, I need to insert all this into the variable that contains the whole file i.e. $content
    thanks,
    z.
      Hi, having read your post a little more carefully, I think maybe this is a bit closer to what you want. $1 holds the match after "value=".
      use strict; my $line = qq(<textarea type="text" value="foo" name="text_message" /> +&nbsp;<%bar%>&nbsp;</p>); if ($line =~ /<textarea .+ value="(.+?)".+(<%.+%>)/) { my $foo = $1; my $bar = $2; $line =~ s/<textarea/<input/; $line =~ s/$bar/$foo/; print $line; }