Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a segment with x's

Status
Not open for further replies.

mpalmer12345

Programmer
Feb 16, 2004
59
US
$text = '<input type=hidden name="avkw" value="fogg">
<input type=hidden name="avkw" value="a be cee">';

Given this bit of code above, I wish to write a line that replaces all words between every instance of value="" with an equal number of xs. So the result should be:

<input type=hidden name="avkw" value="xxxx">
<input type=hidden name="avkw" value="x xx xxx">

I am this far towards a solution, but it doesn't work. I can't figure out why.

$t2 = $text;
$t2 =~ s/value="(^"*?)"/$repl x length($1)/ige;
$text =~ s/(value=")(^"*?)(")/$1$t2$3/ig;

Any assistance would be much appreciated!
 
Capture what you need, and pass it through a tranformation sub.
Code:
$text =~ s/value="([^"]+)"/'value="' . makeXs($1) . '"'/gie;

sub makeXs {
        my $s = shift;
        $s =~ s/\S/x/g;
        return $s;
}
Surely there's other ways, but this is the first that came to mind.

________________________________________
Andrew - Perl Monkey
 
Thanks! It works like a charm! I wasn't familiar with the subroutine approach, so I appreciate learning that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top