我试图在将它与正则表达式匹配后替换字符串中的空格.
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; }
现在当我运行上面的代码时,我尝试修改了一个只读值.现在,如果我将$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"); }