Ruby中的gsub使用`"\'"`。

huangapple go评论67阅读模式
英文:

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(&quot;&#39;&quot;, &quot;&quot;)
 =&gt; &quot;supertest&quot;
str2.gsub("&#39;", "")
 => "supertest"
str2.gsub(&quot;&#39;&quot;, &quot;\\&quot;&quot;)
 =&gt; &quot;super\\test&quot;
str2.gsub("&#39;", "\\&quot;")
 => "super\\test"
str2.gsub(&quot;t&quot;, &quot;ab&quot;)
 =&gt; &quot;super&#39;abesab&quot;
str2.gsub("t", "ab")
 => "super&#39;abesab"
str2.gsub(&quot;t&quot;, &quot;\\b&quot;)
 =&gt; &quot;super&#39;&#39;es&#39;&quot;
str2.gsub("t", "\\b")
 => "super&#39;&#39;es&#39;"
str2.gsub(&quot;t&quot;, &quot;\\&#39;&quot;)
 =&gt; &quot;super&#39;\\&#39;&#39;es&#39;&quot;
str2.gsub("t", "\\&#39;")
 => "super&#39;\\&#39;&#39;es&#39;"
str2.gsub(&quot;&#39;&quot;, &quot;\\&#39;&quot;)
 =&gt; &quot;super&#39;test&quot;
str2.gsub("&#39;", "\\&#39;")
 => "super&#39;test"
str2.gsub(&quot;&#39;&quot;, &quot;\\ &#39;&quot;)
 =&gt; &quot;super\\ &#39;test&quot;
str2.gsub("&#39;", "\\ &#39;")
 => "super\\ &#39;test"
str2.gsub(&quot;&#39;&quot;, &quot;\\&#39;&quot;)
 =&gt; &quot;supertesttest&quot; # I really don&#39;t get it....
str2.gsub("&#39;", "\\&#39;")
 => "supertesttest" # I really don't get it....
str2.gsub(&quot;&#39;&quot;) { &quot;\\&#39;&quot; }
 =&gt; &quot;super\\&#39;test&quot; # This works
str2.gsub("&#39;") { "\\&#39;" }
 => "super\\&#39;test" # This works
英文:

I simply don't understand if this behavior is expected or not.

I am trying to replace the &#39; character in a string by the 2 characters \&#39; (basically escaping the quote by adding a \ before it)

My naive approach was this str.gsub(&quot;&#39;&quot;, &quot;\\&#39;&quot;) but I got weirds results.
I finally got it to work by using str.gsub(&quot;&#39;&quot;) { &quot;\\&#39;&quot; } but I would like to understand the behavior of gsub here.

Here is what I tried :

str2 = &quot;super&#39;test&quot;
 =&gt; &quot;super&#39;test&quot;
str2.gsub(&quot;&#39;&quot;, &quot;\\&#39;&quot;)
 =&gt; &quot;supertesttest&quot;                # &lt;==== ??? Where does that comes from ?

# Further tests are to understand if `gsub` behaves as I would expect, and it does...
str2.gsub(&quot;&#39;&quot;, &quot;&quot;)
 =&gt; &quot;supertest&quot;
str2.gsub(&quot;&#39;&quot;, &quot;\\&quot;)
 =&gt; &quot;super\\test&quot;
str2.gsub(&quot;t&quot;, &quot;ab&quot;)
 =&gt; &quot;super&#39;abesab&quot;
str2.gsub(&quot;t&quot;, &quot;\b&quot;)
 =&gt; &quot;super&#39;\bes\b&quot;
str2.gsub(&quot;t&quot;, &quot;\\b&quot;)
 =&gt; &quot;super&#39;\\bes\\b&quot;
str2.gsub(&quot;t&quot;, &quot;\&#39;&quot;)
 =&gt; &quot;super&#39;&#39;es&#39;&quot;
str2.gsub(&quot;&#39;&quot;, &quot;\&#39;&quot;)
 =&gt; &quot;super&#39;test&quot;
str2.gsub(&quot;&#39;&quot;, &quot;\\ &#39;&quot;)
 =&gt; &quot;super\\ &#39;test&quot;

str2.gsub(&quot;&#39;&quot;, &quot;\\&#39;&quot;)
 =&gt; &quot;supertesttest&quot; # I really don&#39;t get it....
str2.gsub(&quot;&#39;&quot;) { &quot;\\&#39;&quot; }
 =&gt; &quot;super\\&#39;test&quot; # This works

答案1

得分: 2

Sure, here is the translated content:

来自文档的引用:

\&#39; 对应于 $&#39;,其中包含匹配后的字符串。

[...]

请注意,\\ 被解释为转义,即一个单反斜杠。

[...]

如果您想在replacement中编写一个非反向引用字符串\&amp;,您需要首先转义反斜杠,以防止该方法将其解释为反向引用,然后您需要再次转义反斜杠,以防止字符串文字将其消耗掉:&quot;..\\\\&amp;..&quot;

您可能希望使用块形式来避免大量的反斜杠。

英文:

Quote from the docs:

> \&#39; corresponds to $&#39;, 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 \&amp; 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: &quot;..\\\\&amp;..&quot;.
>
> You may want to use the block form to avoid a lot of backslashes.

huangapple
  • 本文由 发表于 2023年5月10日 23:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定