[英]What does '$path =~ s|/$||' do in perl?
I find this piece of code in an opensource project. 我在一个开源项目中找到了这段代码。 The path will be a UNIX path and passed as a parameter.
该路径将是UNIX路径,并作为参数传递。 I was wondering what the following code actually do for me :
我想知道以下代码实际上对我有什么作用:
$path =~ s|/$||;
Thanks! 谢谢!
* *Update: * *更新:
Thanks for telling me the answer, however, I would like to know how the code rewrite in common regular expression matching will be? 感谢您告诉我答案,但是,我想知道普通正则表达式匹配中的代码将如何重写?
I can't figure out the piece enclosed with vertical bar. 我不知道用竖线围起来的那一块。
What is the second last vertical bar means? 倒数第二个竖线是什么意思? Thank you!
谢谢!
This code will remove a slash /
found at the end of the string in $path
. 此代码将删除
$path
字符串末尾的斜杠/
。 So, if you have 所以,如果你有
$path = "foo/bar/";
You will get 你会得到
$path = "foo/bar";
This is a variation on the s///
operator, where the delimiter has been changed from /
to |
这是
s///
运算符的变体,其中定界符已从/
更改为|
, eg s|||
,例如
s|||
. 。 With the
s///
operator, you can exchange the slash /
to almost any other character. 使用
s///
运算符,您可以将斜杠/
为几乎任何其他字符。 For example 例如
s!foo!bar!
s{foo}{bar}
s#foo#bar#
s?foo?bar?
The reason for changing delimiter is usually that such a character appears in the regex, which it does here. 更改定界符的原因通常是这样的字符出现在正则表达式中,在这里它就是这样做的。 If your regex had not changed delimiter, it would look like this:
如果您的正则表达式未更改定界符,则它将如下所示:
s/\/$//
Which is not as readable. 哪个不那么可读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.