From: "Marte Raphael Y. Soliza" Date: 2007-04-27T01:52:27+09:00 Subject: Re: [patch] gsub with backslash characters in replacement string ------=_Part_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 wrote: > > On 4/26/07, Yukihiro Matsumoto wrote: > > > > Hi, > > > > In message "Re: [patch] gsub with backslash characters in replacement > > string" > > on Thu, 26 Apr 2007 17:09:42 +0900, "Adam Bozanich" > > 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 ------=_Part_77089_24876029.1177606344535 Content-Type: text/html; charset=ISO-8859-1 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 ------=_Part_77089_24876029.1177606344535--