Why does String#gsub double content?

半腔热情 提交于 2019-11-26 23:04:51

You're getting tripped up by the specialness of \' inside a regular expression replacement string:

\0, \1, \2, ... \9, \&, \`, \', \+
Substitutes the value matched by the nth grouped subexpression, or by the entire match, pre- or postmatch, or the highest group.

So when you say "\\'", the double \\ becomes just a single backslash and the result is \' but that means "The string to the right of the last successful match." If you want to replace single quotes with escaped single quotes, you need to escape more to get past the specialness of \':

s.gsub("'", "\\\\'")

Or avoid the toothpicks and use the block form:

s.gsub("'") { |m| '\\' + m }

You would run into similar issues if you were trying to escape backticks, a plus sign, or even a single digit.

The overall lesson here is to prefer the block form of gsub for anything but the most trivial of substitutions.

s = "#main = 'quotes'

s.gsub "'", "\\\\'"

Since \it's \\equivalent if you want to get a double backslash you have to put four of ones.

You need to escape the \ as well:

s.gsub "'", "\\\\'"

Outputs

"#main= \\'quotes\\'"

A good explanation found on an outside forum:

The key point to understand IMHO is that a backslash is special in replacement strings. So, whenever one wants to have a literal backslash in a replacement string one needs to escape it and hence have [two] backslashes. Coincidentally a backslash is also special in a string (even in a single quoted string). So you need two levels of escaping, makes 2 * 2 = 4 backslashes on the screen for one literal replacement backslash.

source

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