Project

General

Profile

Actions

Bug #8512

closed

gsub() works incorrect

Added by galnaktar (Oleg K) over 11 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p195 (2013-05-14) [i386-mingw32]
[ruby-core:55430]

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) over 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) over 11 years ago

This should be documented in Regexp's overview though.

Updated by Hanmac (Hans Mackowiak) over 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) over 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) over 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("\") { "\\" } #=> "\\"

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0