[英]Gsub causing part of string to be substituted
I want to replace all occurrences of a single quote ( '
) with backslash single quote ( \\'
). 我想更换一个单引号(所有出现的
'
用反斜杠单引号() \\'
)。 I tried doing this with gsub
, but I'm getting partial string duplication: 我尝试用
gsub
做这个,但我得到部分字符串重复:
a = "abc 'def' ghi"
a.gsub("'", "\\'")
# => "abc def' ghidef ghi ghi"
Can someone explain why this happens and what a solution to this is? 有人可以解释为什么会发生这种情况,解决方案是什么?
It happens because "\\\\'"
has a special meaning when it occurs as the replacement argument of gsub
, namely it means the post-match substring. 之所以会发生这种情况是因为
"\\\\'"
在作为gsub
的替换参数出现时具有特殊含义,即它意味着匹配后的子字符串。
To do what you want, you can use a block: 要做你想做的事,你可以使用一个块:
a.gsub("'"){"\\'"}
# => "abc \\'def\\' ghi"
Notice that the backslash is escaped in the string inspection, so it appears as \\\\
. 请注意,反斜杠在字符串检查中被转义,因此它显示为
\\\\
。
Your "\\\\'"
actually represents a literal \\'
because of the backslash escaping the next backslash. 你的
"\\\\'"
实际上代表了一个字面值\\'
因为反斜杠逃避了下一个反斜杠。 And that literal \\'
in Ruby regex is actually a special variable that interpolates to the part of the string that follows the matched portion . 和文字
\\'
在红宝石正则表达式实际上是插值以下面的匹配部分字符串的部分的特殊变量。 So here's what's happening. 所以这就是发生的事情。
abc 'def' ghi
^
The caret points to the first match, '
. 插入符号指向第一场比赛,
'
。 Replace it with everything to its right, ie def' ghi
. 将其替换为右侧的所有内容, 即
def' ghi
。
abc def' ghidef' ghi
++++++++
Now find the next match: 现在找到下一场比赛:
abc def' ghidef' ghi
^
Once again, replace the '
with everything to its right, ie ghi
. 再一次,将
'
一切都改为右边, 即 ghi
。
abc def' ghidef ghi ghi
++++
It's possible you just need a higher dose of escaping: 您可能需要更高剂量的逃逸:
a.gsub(/'/, "\\\\'" )
Result : 结果 :
abc \\'def\\' ghi
abc \\'def \\'ghi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.