------art_77089_24876029.1177606344535
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 4/26/07, Adam Bozanich <adam.boz / gmail.com> wrote:
>
> On 4/26/07, Yukihiro Matsumoto <matz / ruby-lang.org> wrote:
> >
> > Hi,
> >
> > In message "Re: [patch] gsub with backslash characters in replacement
> > string"
> >     on Thu, 26 Apr 2007 17:09:42 +0900, "Adam Bozanich" <adam.boz / gmail.com >
> > writes:
> >
> > |Hello, spotted this one the other day:
> > |
> > |irb(main):001:0> '\\'.gsub('\\','\\\\')
> > |"\\"
> > |irb(main):002:0> '\\'.gsub('\\','\\\\\\')
> > |"\\\\"
> > |
> > |The following patch seems to fix things on my system
> >
> > It was intentional to escape backslashes in replacement string.
> > If it were a bug, how do we escape backslashes?  Any idea?
> >
> >                                                         matz.
> >
> >
> Not sure that I'm understanding you here.
>
> let's try again.
>
> test.rb:
>
> 1.upto(6) do |x|
>     print "a".gsub("a","\\"*x).size , " "
> end
>
> print "a".gsub("a","\n")
>
> before patch output is:
> 1 1 2 2 3 3
>
> after patch output is:
> 1 2 3 4 5 6
>
> Note that the newline works in both cases.
>
> -Adam
>

It has something to do with replacing groups in regular expressions. For
example, you can do:
>> "abc".gsub(/(.)/, "\\1,")
"a,b,c,"

So, to escape literal backslashes in replacements, we need four backslashes:
\\\\ # actual code
\\ # actual string
\ # actual replacement
Now if we have an odd number of actual backslash characters, then the last
one will be treated as a literal:
\\\\\\ # actual code
\\\ # actual string
\\ # actual replacement

What we can do is to not double-escape the backslashes if the first argument
is non-regex. Though that maybe a little confusing especially if you expect
your first argument to be both string and regex.


-- 
"Life is unfair... but beautiful."
Scarlette Krimson

------art_77089_24876029.1177606344535
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 4/26/07, <b classmail_sendername">Adam Bozanich</b> &lt;<a hrefailto:adam.boz / gmail.com">adam.boz / gmail.com</a>&gt; wrote:<div><span classmail_quote"></span><blockquote classmail_quote" styleorder-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<span class">On 4/26/07, <b classmail_sendername">Yukihiro Matsumoto</b> &lt;<a hrefailto:matz / ruby-lang.org" targetblank" onclicketurn top.js.OpenExtLink(window,event,this)">matz / ruby-lang.org</a>&gt; wrote:
</span><div><span class「 ゜アアイイキウカーイキ゜ア「セシセシ ゜「セ
シッセシ ゜「 ュコ ア  ィイーエャ イーエャ イーエゥサ コ ー ー ー ーョクサ ュコ アサ「セ
ネャシセシセノ  ヲサメコ ロン       ヲサシセヲサヲサヲサヲサ ヤャ イカ チ イーーキ アキコーケコエイ ォーケーーャ ヲサチ ツヲサ ヲサシ コョタョ「 「  ョョマナフィャャゥ「セ

ョタョ
シッセヲサ コシセシセネャ      コシセシセィゥコーーアコーヲサ ヲ」ウケサワワヲ」ウケサョィヲ」ウケサワワヲ」ウケサャヲ」ウケサワワワワヲ」ウケサゥシセサ ヲサワワヲサシセィゥコーーイコーヲサ ヲ」ウケサワワヲ」ウケサョィヲ」ウケサワワヲ」ウケサャヲ」ウケサワワワワワワヲ」ウケサゥ
シセサ ヲサワワワワヲサシセシセヤ         シセシセノ        ョシセノ    ャ     ソヲサヲサチ ソ
シセシセヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサヲサョシセシセシッセシッセシセシッセシッセホ   ノヲ」ウケサ   ョシセシセヲ」ウケサ  ョシセシセョコシセシセアョィカゥ  
シセヲサヲサヲサ  ヲサヲサョィヲサヲサャヲサワワヲサェゥョ ャ ヲサ ヲサ
シセシセシセ ヲサヲサョィヲサヲサャヲサワヲサゥシセシセ   コシセア ア イ イ ウ ウ シセシセ   コシセア イ ウ エ オ カ シセシセホ       ョ シセシ 「セ
シセ
ュチシセ
シッセシッセシッセシセノ          ョ ニ ャ   コシセヲサヲサ ヲサヲサョィッィョゥッャ ヲサワワアャヲサゥシセサ ヲサャャャヲサシセシセモャ      ャ    コ
シセワワワワ 」  シセワワ 」  シセワ 」  シセホ          ャ          コシセワワワワワワ 」  シセワワワ 」  
シセワワ 」  シセシセラ       ュ        ュョ ヤ                  ョ
シセシセシセュュ シセヲサフ  ョョョ  ョヲサシセモ ヒ

ュュュュュュ゜キキークケ゜イエクキカーイケョアアキキカーカウエエオウオュュ