简体   繁体   English

perl正则表达式解释

[英]perl regular expression explanations

I am completely lost on this line of perl code 我完全迷失在这行perl代码上

$path =~ s|^\./|~/|; #change the path for prettier output

I am assuming it has to do with regex. 我假设它与正则表达式有关。 I have some understanding of regex but i just cant seem to figure this one out. 我对正则表达式有一些了解,但我似乎无法想出这一点。

what is =~ and why is there s and how does regex expressed in perl? 什么是=~以及为什么有正则表达式在perl中表达?

=~ is a binding operator. =~是一个绑定运算符。 It applies the substitution (hence the s ) to the variable $path . 它将替换(因此s )应用于变量$path The substitution has two parts - a regular expression and the replacement. 替换有两个部分 - 正则表达式和替换。 They are delimited by the | 它们由|分隔 character in this case. 在这种情况下的字符。 The regular expression is 正则表达式是

^\./

^ stands for the beginning of the string. ^代表字符串的开头。 \\. stands for a literal dot, / stands for itself. 代表字面点, /代表自己。 So, ./ at the beginning of the string is replaced by ~/ . 因此,字符串开头的./~/替换。

the =~ binds a scalar expression to a pattern match, the s is for replacement =~将标量表达式绑定到模式匹配, s用于替换

what its doing is matching start of line with a ./ then replacing it with a ~/ 它的作用是匹配起始行与./然后用〜/替换它

as far as the | 至于| pipes, you can use any non-whitespace character to delimit parts of the regex you can use ^ or & or q or m or { whatever.. most people use / for readability but for cases where you might match on / use something else. 管道,您可以使用任何非空白字符来界定正则表达式的部分,您可以使用^或&或q或m或{what ..大多数人使用/为了可读性,但适用于您可能匹配/使用其他内容的情况。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM