Perl字符串子

bmw*_*128 4 perl

我想用路径替换一些东西C:\foo,所以我:

s/hello/c:\foo
Run Code Online (Sandbox Code Playgroud)

但那是无效的.我需要逃脱一些角色吗?

pil*_*row 5

我能看到的两个问题.

您的第一个问题是您的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)
Run Code Online (Sandbox Code Playgroud)

您提出的第二个问题是,\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
Run Code Online (Sandbox Code Playgroud)

看看治疗报价及报价般的运营商perlop获取更多信息.