问题描述
请考虑以下代码段:
puts'hello'.gsub(/.+/,'\0 \ \0 \\\0 \\\\0')
这将打印():
hello hello \0 \0
非常惊讶,因为我希望看到像这样的东西:
hello \0 \hello \\ 0
我的论据是 \
一个转义字符,所以你写 \\
来获得一个字面反斜杠,因此 \\0
是一个字面反斜杠 \
后跟 0
等等。显然这不是如何 gsub
正在解释它,所以有人可以解释发生了什么吗?
我需要做什么才能获得我想要的替换? p >
使用单引号而不是双引号时,转义是有限的:
put'sinlge\\\
quote'
putsdouble\\\
quote
\0
是空字符(用于C确定字符串的结尾),其中 '\0'
是\\0
,因此'hello'.gsub(/。 + /,'\0')
和'hello'.gsub(/.+/,\\\0)
return hello
,但'hello'.gsub(/.+/,\0)
返回 \000
。现在'hello'.gsub(/.+/,'\\\0')
返回'hello'
是红宝石试图处理程序员,而不是保持单引号和双引号之间的区别。实际上,这与 gsub
无关:'\0'==\\0
和'\\0'==\\0
。遵循这个逻辑,无论你想到什么,这是ruby看到其他字符串:'\\\\0'
和 '\\\\\0'
等于\\\\0
,(打印时)给出你 \\0
。由于gsub使用 \x
来插入匹配号x,所以您需要一种方法来逃避 \x
\\x
或其字符串表示形式:\\\\\x
。因此,
put'hello'.gsub(/ 。+ /,\\0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ )
确实导致
hello \0 \hello \\0
Consider the following snippet:
puts 'hello'.gsub(/.+/, '\0 \\0 \\\0 \\\\0')
This prints (as seen on ideone.com):
hello hello \0 \0
This was very surprising, because I'd expect to see something like this instead:
hello \0 \hello \\0
My argument is that \
is an escape character, so you write \\
to get a literal backslash, thus \\0
is a literal backslash \
followed by 0
, etc. Obviously this is not how gsub
is interpreting it, so can someone explain what's going on?
And what do I have to do to get the replacement I want above?
Escaping is limited when using single quotes rather then double quotes:
puts 'sinlge\nquote'
puts "double\nquote"
"\0"
is the null-character (used i.e. in C to determine the end of a string), where as '\0'
is "\\0"
, therefore both 'hello'.gsub(/.+/, '\0')
and 'hello'.gsub(/.+/, "\\0")
return "hello"
, but 'hello'.gsub(/.+/, "\0")
returns "\000"
. Now 'hello'.gsub(/.+/, '\\0')
returning 'hello'
is ruby trying to deal with programmers not keeping the difference between single and double quotes in mind. In fact, this has nothing to do with gsub
: '\0' == "\\0"
and '\\0' == "\\0"
. Following this logic, whatever you might think of it, this is how ruby sees the other strings: both '\\\0'
and '\\\\0'
equal "\\\\0"
, which (when printed) gives you \\0
. As gsub uses \x
for inserting match number x, you need a way to escape \x
, which is \\x
, or in its string representation: "\\\\x"
.
Therefore the line
puts 'hello'.gsub(/.+/, "\\0 \\\\0 \\\\\\0 \\\\\\\\0")
indeed results in
hello \0 \hello \\0
这篇关于gsub中的反斜杠(转义和反向引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!