Perl regex with pipes

自闭症网瘾萝莉.ら 提交于 2021-02-05 06:10:03

问题


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 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...

Thanks


回答1:


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/;  

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.



来源:https://stackoverflow.com/questions/31830613/perl-regex-with-pipes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!