substitution - How to get the substituted text of perl (s//)? -
i use following code substitution. there way retrieve text substituted? in example, want string _5p_outsuffix.txt
.
/tmp$ ./main.pl xxx_5p_outsuffix.txt /tmp$ cat main.pl #!/usr/bin/env perl use strict; use warnings; $filename="xxx_5p_insuffix.txt"; $insuffix="_((5|3)p)_insuffix\.txt"; $outsuffix = '_$1_outsuffix.txt'; $filename =~ s/$insuffix$/qq{"$outsuffix"}/ee; print "$filename\n";
you should capture match string:
$filename =~ s/($insuffix)$/qq{"$outsuffix"}/ee; print "$filename\n"; print "substituted: $1\n";
since introduces level of braces, have adjust numbers of captures used in replacement string. alternatively built-in ${^match}
variable contains string matched last successful regex using /p
modifier. like
$filename =~ s/$insuffix$/qq{"$outsuffix"}/eep; print "$filename\n"; print "substituted: ${^match}\n";
(you shouldn't use $&
purpose makes regular expressions in program substantially slower.)
Comments
Post a Comment