gpt4 book ai didi

regex - 带管道的 Perl 正则表达式

转载 作者:行者123 更新时间:2023-12-01 11:31:09 25 4
gpt4 key购买 nike

我不是一个 perl 僧侣,所以如果你能帮助我消化这个正则表达式(如果它是一个)的作用是什么?

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

Perl 并不是我的日常工具,所以我对知识很害羞 - 我猜它会将匹配替换为 var 值,但猜测不是正确的方法 - 管道让我失望......

谢谢

最佳答案

在 Perl 中,您通常会使用/作为正则表达式中的定界符。

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

但是您可以看到,对于路径来说,这是有问题的,因为您必须转义所有内容。

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

因此,Perl 允许您指定一个不同的定界符作为“s”之后的字符,因此:

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

参见 the documentation获取更多信息。

关于regex - 带管道的 Perl 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31830613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com