我有以下字符串:
猫狗(狐狸)毛毛虫熊狡猾
我需要将这句话中的“cat”和“fox”单词替换为“animal”
use warnings;
use strict;
# declare your vars with 'my' for lexical scope
my $inputFile = "somefile";
my $outputFile = "someotherfile";
my $Str1="cat";
my $Str2="fox";
my $NewStr="animal";
# use 3-arg lexically scoped open
open(my $F1, "<", $inputFile) or die "Error: $!";
open(my $F2, ">", $outputFile) or die "Error: $!";
while (my $line = <$F1>) {
# surround with word boundary '\b'
# NewStr is missing the "$"
$line =~ s/\b(?:$Str1|$Str2)\b/$NewStr/g;
print $F2 "$line";
}
# close your file handles
close $F1;
close $F2;
没关系,但是如果我在括号“()”中有一些词。假设我有“这样的单词狐狸(狐狸)。我有结果 - 带括号的“(动物)”。替换单词时如何删除括号?