Bug #8512
closedgsub() works incorrect
Description
irb(main):005:0> "\".gsub("\", "\\").length
=> 1
irb(main):006:0> "\".gsub("\", "XX").length
=> 2
bug is duplicated with rejected bug #8511
Updated by Eregon (Benoit Daloze) about 11 years ago
- Status changed from Open to Rejected
This is due to Regexp replace syntax and literal strings.
In literal strings, you need two \ to produce one \ character (a single is the start of an escape character like \t, \n, ...).
And in Regexp replacement strings, you need to escape the \ (a single one is the beginning of a special replacement sequence like \1,&,...).
So that makes 4 \ for one produced in literal replacement strings:
"\".gsub("\", "\\").length
=> 1
"\".gsub("\", "\\\\").length
=> "\\"
Not so nice, but definitely expected behavior.
Updated by Eregon (Benoit Daloze) about 11 years ago
This should be documented in Regexp's overview though.
Updated by Hanmac (Hans Mackowiak) about 11 years ago
=begin
the docs says:
((If replacement is a String it will be substituted for
the matched text. It may contain back-references to the pattern's capture
groups of the form \\d, where d is a group number, or \\k, where n is
a group name. If it is a double-quoted string, both back-references must be
preceded by an additional backslash.))
so you need more "" in your string
=end
Updated by galnaktar (Oleg K) about 11 years ago
Hanmac (Hans Mackowiak) wrote:
=begin
the docs says:
((If replacement is a String it will be substituted for
the matched text. It may contain back-references to the pattern's capture
groups of the form \\d, where d is a group number, or \\k, where n is
a group name. If it is a double-quoted string, both back-references must be
preceded by an additional backslash.))so you need more "" in your string
=end
Thanks for the explanation.
Updated by phluid61 (Matthew Kerwin) about 11 years ago
For the record, you can also use the hash or block replacement forms, because those doesn't use regexp back-references:
"\".gsub("\", "\"=>"\\") #=> "\\"
"\".gsub("\") { "\\" } #=> "\\"