in reply to Selective replace based on user input
Assuming you're ok with loading the entire file into memory, you can use the following:
or$str =~ s{$pat}{ prompt( $_, $-[0], $+[0]-$-[0], $&, $repl, $`, $' ) ? $repl : $& }eg
use String::Substitution qw( interpolate_match_vars last_match_vars ); $str =~ s{$pat}{ my $true_repl = interpolate_match_vars( $repl, last_match_vars() ); prompt( $_, $-[0], $+[0]-$-[0], $&, $true_repl, $`, $' ) ? $true_repl : $& }eg
The latter supports interpolation of numerical captures such as $1 and ${1} in $repl, though you might need to escape some dollar signs and backslashes.
It's up to you to provide $str (the contents of the file), $pat (the pattern for which to search as a string or compiled regex), $repl (the replacement string) and prompt.
Args passed to prompt:
Have prompt return a true value to replace, or a false value to skip the replacement.
Update: Added second version and a lot of info.
|
---|