我想用类似 C:\foo
的路径替换一些东西, 所以我:
s/hello/c:\foo
但那是无效的。
我需要转义一些字符吗?
最佳答案
我能看到的两个问题。
您的第一个问题是您的 s///
更换未终止:
s/hello/c:\foo # fatal syntax error: "Substitution replacement not terminated"
s/hello/c:\foo/ # syntactically okay
s!hello!c:\foo! # also okay, and more readable with backslashes (IMHO)
您问的第二个问题是
\f
被视为换页转义序列(ASCII 0x0C),就像双引号一样,这不是您想要的。您可以转义反斜杠,或者让变量插值“隐藏”问题:
s!hello!c:\\foo! # This will do what you want. Note double backslash.
my $replacement = 'c:\foo' # N.B.: Using single quotes here, not double quotes
s!hello!$replacement!; # This also works
查看
perlop
中对 Quote 和 Quote-like Operators 的处理想要查询更多的信息。
关于Perl 字符串子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114584/