Reputation: 7087
Can someone tell me why my script below search/replace when I am using \Q$btype
when it works when I hard code with center
instead?
The script is suppose to insert $$
after \end{center}
.
#!/usr/bin/perl
my $line = '\end{tabular}
\end{center}
end:text
';
my $btype = "center";
$line =~ s/\\end\{\Q$btype\}/\\end\{\Q$btype\}\$\$/g;
print "$line\n";
Upvotes: 0
Views: 85
Reputation: 91385
You need to stop the escaping:
$line =~ s/\\end\{\Q$btype\E\}/\\end\{$btype\}\$\$/g;
# here __^^
it could be reduce to:
$line =~ s/\\end\{\Q$btype\E\}\K/\$\$/g; # 5.10+
or
$line =~ s/(\\end\{\Q$btype\E\})/$1\$\$/g;
or
$line =~ s/\\end\{\Q$btype\E\}/$&\$\$/g;
From ThisSuitIsBlackNot's comment:
Don't use it with perl before v5.20 because of performance issue.
or
$line =~ s/(?<=\\end\{\Q$btype\E\})/\$\$/g;
Upvotes: 6