検索値に括弧が必要な場合は、括弧をエスケープするバックスラッシュをエスケープする必要があります。置換の括弧は問題になりませんが、正規表現でのグループ化に使用されるため、一致します。
値が割り当てられていると仮定すると$table
、検索して置換するテキストのみを渡したいとします。
次の例では、文字列を に(hello)
置き換えます。hi
<table-wrap-foot>(hello)</table-wrap-foot>
#!/usr/bin/perl
$foot = "(hello)";
print $foot . "\n"; # $foot = (hello)
# replace all ( and ) with \( and \)
$foot =~ s/(\(|\))/\\$1/sg; # $foot = \(hello\)
print $foot . "\n";
# replace with "hi"
$table = "<table-wrap-foot>(hello)</table-wrap-foot>";
print $table . "\n";
$table =~ s/<table-wrap-foot>($foot)</table-wrap-foot>/hi/sg;
print $table;
出力:
> perl test.pl
(hello)
\(hello\)
<table-wrap-foot>(hello)</table-wrap-foot>
hi