Perl正则表达式与管道

时间:2015-08-05 11:00:15

标签: regex perl

我不是一个perl和尚,所以如果你能帮助我消化这个正则表达式(如果是那个)的话吗?

my $pathHere = "/some/path/to/file";
my $pathThere = "/some/path/";
$pathHere =~ s|$pathThere||;

Perl并不是我的日常工具,所以我对知识非常害羞 - 我猜它将匹配变为var值,但猜测不是要走的路 - 管道让我失望......

由于

1 个答案:

答案 0 :(得分:5)

在Perl中,你通常使用/作为regexp中的分隔符。

$pathHere =~ s/abc/def/;   # replace 'abc' with 'def'

然而,你可以看到,对于路径而言,这是有问题的,因为你必须逃避一切。

$pathHere =~ s/my\/path\/here/my\/newpath\/there/;  

因此,Perl允许您指定一个不同的分隔符作为'之后的字符,因此:

$pathHere =~ s|my/path/here|my/newpath/there|;

有关详细信息,请参阅the documentation