[ruby-core:77054] [Ruby trunk Bug#12703][Rejected] String.gsub returns unexpected results with numeric match characters

From: nobu@...
Date: 2016-08-25 05:52:08 UTC
List: ruby-core #77054
Issue #12703 has been updated by Nobuyoshi Nakada.

Status changed from Open to Rejected

What you want is `"z'z".gsub(/(')/, '\\\\\1')`.
You need to escape twice, for a literal, and for `gsub`.

----------------------------------------
Bug #12703: String.gsub returns unexpected results with numeric match characters
https://bugs.ruby-lang.org/issues/12703#change-60278

* Author: Nathan LaVeck
* Status: Rejected
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.4.0dev (2016-08-25 trunk 56006)
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
I'm unable to prepend a backslash to numeric group matches using gsub

~~~
irb(main):053:0> "z'z".gsub(/(')/, '\\1')
=> "z'z"
~~~

Expected Result

~~~
=> "z\'z"
~~~

Adding an extra backslash gives me this:

~~~
irb(main):056:0> "z'z".gsub(/(')/, '\\\1')
=> "z\\1z"
~~~


Tested on Ruby version 2.4.0



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next