in reply to Re: $1 in variable regex replacement string
in thread $1 in variable regex replacement string
Very nice! So all wrapped up and ready to go you would have:
my $str = "abcdefghijafjafjkagjakg"; my $pat = '(a.)'; my $repl = '$1 '; print munge($str,$pat,$repl); =head2 munge( STRING, PATTERN, REPLACEMENT ) The munge function takes three arguments and returns a string. The first argument is the STRING to be modified. The modification is performed by s/PATTERN/REPLACEMENT/g. The main advantage of this function is that REPLACEMENT can contain $1, $2 etc that have been captured by PATTERN These values are safely interpolated prior to the substitution being made. =cut sub munge { my($str, $pat, $repl) = @_; $str =~ s/$pat/_safeswitch($repl)/eg; return $str; } # used by munge function to safely interpolate # $1, $2 etc into the replacement string sub _safeswitch { my @P = (undef,$1,$2,$3,$4,$5,$6,$7,$8,$9); $_[0] =~ s/\$(\d)/$P[$1]/g; $_[0]; }
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: $1 in variable regex replacement string
by belden (Friar) on Feb 13, 2003 at 01:32 UTC | |
by tadman (Prior) on Feb 14, 2003 at 03:26 UTC | |
by belden (Friar) on Feb 15, 2003 at 01:01 UTC |