我正在尝试whitespaces在将其与正则表达式匹配后替换为字符串.
my $string = "watch download Buffy the Vampire Slayer Season 1 Episode 1 Gorillavid";
if ($string =~ m!(Season\s\d+\sEpisode\s\d+)!){
$1 =~ s!\s+!!g;
say $1;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行上面的代码时,我得到了Modification of a read-only value attempted.现在,如果我将值存储$1在变量中,而不是尝试对该变量执行替换,则可以正常工作.
那么,有没有什么方法可以在不创建新临时变量的情况下执行替换.
PS:有人可以告诉我如何将上面的代码写成一行,因为我无法:)
不要乱用特殊变量,只需捕获所需的数据,同时自己构建输出.
$string = "watch download Buffy the Vampire Slayer Season 1 Episode 1 Gorillavid";
if ($string =~ m!Season\s(\d+)\sEpisode\s(\d+)!){
say("Season$1Episode$2");
}
Run Code Online (Sandbox Code Playgroud)