英文:
Ruby gsub with `"\'"`
问题
I simply don't understand if this behavior is expected or not.
我不确定这种行为是否符合预期。
I am trying to replace the '
character in a string by the 2 characters \'
(basically escaping the quote by adding a \
before it)
我试图将字符串中的 '
字符替换为两个字符 \'
(基本上是通过在引号前面添加 \
来进行转义)。
My naive approach was this str.gsub("'", "\\'")
but I got weirds results.
我的天真做法是这样的 str.gsub("'", "\\'")
,但我得到了奇怪的结果。
I finally got it to work by using str.gsub("'") { "\\'" }
but I would like to understand the behavior of gsub here.
最终,我通过使用 str.gsub("'") { "\\'" }
得以正常工作,但我想了解这里gsub的行为。
Here is what I tried :
这是我尝试过的内容:
str2 = "super'test"
=> "super'test"
str2.gsub("'", "\\'")
=> "supertesttest" # <==== ??? Where does that comes from ?
str2 = "super'test"
=> "super'test"
str2.gsub("'", "\\'")
=> "supertesttest" # <==== ??? Where does that comes from ?
Further tests are to understand if gsub
behaves as I would expect, and it does...
进一步的测试是为了了解 gsub
是否表现如我所期望,它确实如此...
str2.gsub("'", "")
=> "supertest"
str2.gsub("'", "")
=> "supertest"
str2.gsub("'", "\\"")
=> "super\\test"
str2.gsub("'", "\\"")
=> "super\\test"
str2.gsub("t", "ab")
=> "super'abesab"
str2.gsub("t", "ab")
=> "super'abesab"
str2.gsub("t", "\\b")
=> "super''es'"
str2.gsub("t", "\\b")
=> "super''es'"
str2.gsub("t", "\\'")
=> "super'\\''es'"
str2.gsub("t", "\\'")
=> "super'\\''es'"
str2.gsub("'", "\\'")
=> "super'test"
str2.gsub("'", "\\'")
=> "super'test"
str2.gsub("'", "\\ '")
=> "super\\ 'test"
str2.gsub("'", "\\ '")
=> "super\\ 'test"
str2.gsub("'", "\\'")
=> "supertesttest" # I really don't get it....
str2.gsub("'", "\\'")
=> "supertesttest" # I really don't get it....
str2.gsub("'") { "\\'" }
=> "super\\'test" # This works
str2.gsub("'") { "\\'" }
=> "super\\'test" # This works
英文:
I simply don't understand if this behavior is expected or not.
I am trying to replace the '
character in a string by the 2 characters \'
(basically escaping the quote by adding a \
before it)
My naive approach was this str.gsub("'", "\\'")
but I got weirds results.
I finally got it to work by using str.gsub("'") { "\\'" }
but I would like to understand the behavior of gsub here.
Here is what I tried :
str2 = "super'test"
=> "super'test"
str2.gsub("'", "\\'")
=> "supertesttest" # <==== ??? Where does that comes from ?
# Further tests are to understand if `gsub` behaves as I would expect, and it does...
str2.gsub("'", "")
=> "supertest"
str2.gsub("'", "\\")
=> "super\\test"
str2.gsub("t", "ab")
=> "super'abesab"
str2.gsub("t", "\b")
=> "super'\bes\b"
str2.gsub("t", "\\b")
=> "super'\\bes\\b"
str2.gsub("t", "\'")
=> "super''es'"
str2.gsub("'", "\'")
=> "super'test"
str2.gsub("'", "\\ '")
=> "super\\ 'test"
str2.gsub("'", "\\'")
=> "supertesttest" # I really don't get it....
str2.gsub("'") { "\\'" }
=> "super\\'test" # This works
答案1
得分: 2
Sure, here is the translated content:
来自文档的引用:
\'
对应于$'
,其中包含匹配后的字符串。[...]
请注意,
\\
被解释为转义,即一个单反斜杠。[...]
如果您想在
replacement
中编写一个非反向引用字符串\&
,您需要首先转义反斜杠,以防止该方法将其解释为反向引用,然后您需要再次转义反斜杠,以防止字符串文字将其消耗掉:"..\\\\&.."
。您可能希望使用块形式来避免大量的反斜杠。
英文:
Quote from the docs:
> \'
corresponds to $'
, which contains string after match.
>
> [...]
>
> Note that \\
is interpreted as an escape, i.e., a single backslash.
>
> [...]
>
> If you want to write a non-back-reference string \&
in replacement
, you need first to escape the backslash to prevent this method from interpreting it as a back-reference, and then you need to escape the backslashes again to prevent a string literal from consuming them: "..\\\\&.."
.
>
> You may want to use the block form to avoid a lot of backslashes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论