带管道的 Perl 正则表达式 [英] Perl regex with pipes

查看:72
本文介绍了带管道的 Perl 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不完全是一个 perl 和尚,所以如果你能帮我消化一下这个正则表达式(如果有的话)有什么作用?

I'm not exactly a perl monk, so if you could help me digest what does this regex(if it is one) do?

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

Perl 不完全是我的日常工具,所以我对知识很害羞 - 我猜它会取代 var 值的匹配,但猜测不是要走的路 - 管道让我失望...

Perl is not exactly my everyday tool, so I am quite shy on knowledge - I guess it subs the match to the var value, but guessing is not the way to go - the pipes throw me off...

谢谢

推荐答案

在 Perl 中,您通常会使用/作为正则表达式中的分隔符.

In Perl you'd normally use the / as a delimiter in the regexp.

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

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

However you can see that for paths, that's problematic, since you'd have to escape everything.

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

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

Consequently Perl allows you to specify a different delimiter as the character after the 's', hence:

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

有关详细信息,请参阅文档.

See the documentation for more information.

这篇关于带管道的 Perl 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆