[英]perl regular expressions substitue
I'm new to perl and I found this expressions bit difficult to understand 我是perl的新手,我觉得这个表达有点难以理解
$a =~ s/\'/\'\"\'\"\'/g
Can someone help me understand what this piece of code does? 有人可以帮我理解这段代码的作用吗?
It is not clear which part of it is a problem. 目前尚不清楚哪一部分是问题。 Here is a brief mention of each.
这里简要提一下每一个。
The expression $str =~ s/pattern/replacement/
is a regular expression , replacing the part of string $str
that is matched by the "pattern", by the "replacement". 表达式
$str =~ s/pattern/replacement/
是一个正则表达式 ,用“替换”替换了由“pattern”匹配的字符串$str
的一部分。 With /g
at the end it does so for all instances of the "pattern" found in the string. 最后用
/g
对字符串中的“模式”的所有实例都这样做。 There is far, far more to it -- see the tutorial , a quick start , the reference , and full information in perlre and perlop . 远远不止于此 - 请参阅教程 , 快速 入门 , 参考资料以及perlre和perlop中的完整信息。
Some characters have a special meaning, in particular when used in the "pattern". 某些字符具有特殊含义,特别是在“模式”中使用时。 A common set is
.^$*+?()[{\\|
一个常见的集合是
.^$*+?()[{\\|
, but this depends on the flavor of regex. ,但这取决于正则表达式的味道 。 Also, whatever is used as a delimiter (commonly
/
) has to be escaped as well. 此外,任何用作分隔符(通常为
/
)的东西也必须被转义。 See documentation and for example this post . 请参阅文档,例如本文 。 If you want to use any one of those characters as a literal character to be found in the string, they have to be escaped by the backslash.
如果要在字符串中使用这些字符中的任何一个作为文字字符,则必须使用反斜杠对其进行转义 。 (Also see about escaped sequences .) In your example, the
'
is escaped, and so are the characters used as the replacement. (另请参阅转义序列 。)在您的示例中,
'
被转义,用作替换的字符也是如此。
The thing is, these in fact don't need to be escaped. 事实上,这些实际上并不需要被转义。 So this works
这样可行
use strict;
use warnings;
my $str = "a'b'c";
$str =~ s/'/'"'/g;
print "$str\n";
It prints the desired a'"'b'"'c
. 它打印出所需
a'"'b'"'c
。 Note that you still may escape them and it will work. 请注意,你仍然可以逃避它们,它会工作。 One situation where
'
is indeed very special is in a one liner , where it delimits the whole piece of code to submit to Perl via shell, to be run. '
确实非常特殊的一种情况是在一个单行中 ,它将整个代码分隔为通过shell提交给Perl,以便运行。 (However, merely escaping it there does not work.) Altogether, the expression you show does the replacement just as in the answer by Jens , but with a twist that is not needed. (但是,仅仅逃避它就不起作用了。)总而言之,你所表达的表达式就像Jens的回答一样,但是有一个不需要的扭曲。
A note on a related feature: you can use nearly anything for delimiters, not necessarily just /
. 关于相关功能的注释:您几乎可以使用任何分隔符,而不仅仅是
/
。 So this |patt|repl|
所以这个
|patt|repl|
is fine, and nicely even this is: {patt}{repl}
(unless they are used inside). 很好,甚至这是:
{patt}{repl}
(除非它们在里面使用)。 This is sometimes extremely handy, improving readability a lot. 这有时非常方便,可以提高可读性。 For one thing, then you don't have to escape the
/
itself inside the regex. 首先,你不必在正则表达式中逃避
/
本身。
I hope this helps a little. 希望这会有帮助。
All the \\
in that are useless (but harmless), so it is equivalent to s/'/'"'"'/g
. 所有的
\\
都是无用的(但无害),所以它相当于s/'/'"'"'/g
。 It replaces every '
with '"'"'
in the string. 它取代了字符串中的每个
'
with '"'"'
。
This is often used for shell quoting. 这通常用于shell引用。 See for example https://stackoverflow.com/a/24869016/17389
请参阅https://stackoverflow.com/a/24869016/17389
这个代码的和平取代每个单引号与'"'"'
Can be simplified by removing the needless back slashes $a =~ s/'/"'"'/g 可以通过删除不必要的反斜杠来简化$ a = ~s /'/“'”'/ g
Every ' will we replaced with "'"' 每一个'我们都会用''''代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.