Project

General

Profile

Misc #16091

gsub

Added by thiaguerd (thiago feitosa) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:94218]

Description

a = "test ?"
b = "?"
c = "\\&"
a.gsub(b,c)
#1

Updated by thiaguerd (thiago feitosa) about 1 year ago

  • Subject changed from sub to gsub
#2

Updated by nobu (Nobuyoshi Nakada) about 1 year ago

  • Status changed from Open to Feedback
  • Description updated (diff)

Updated by shyouhei (Shyouhei Urabe) about 1 year ago

Really sorry that I have to say I don't understand this report at all. Please tell us about your problem a bit more in detail.

Updated by thiaguerd (thiago feitosa) about 1 year ago

shyouhei (Shyouhei Urabe) wrote:

Really sorry that I have to say I don't understand this report at all. Please tell us about your problem a bit more in detail.

the replacement doesn't happen

try in python and see the diff

a = "test ?"
b = "?"
c = "\\&"
a.replace(b,c)

Updated by chrisseaton (Chris Seaton) about 1 year ago

thiaguerd (thiago feitosa) I don't understand - the String#replace method only takes one argument - you're calling it with two so that's not going to work for a start. Secondly, the method doesn't replace occurrences of one string with another, which is what I think you think it does. It replaces the whole string with another string.

https://ruby-doc.org/core-2.6.3/String.html#method-i-replace

So your code doesn't work because that's just not what this method does or is documented to do in Ruby.

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Status changed from Feedback to Rejected

This is not a bug. Ruby treats \& in a replacement string specially, representing the entire matched string (note that "\\&" is the same as '\&'):

"test ?".sub("?", "1\\&2")
=> "test 1?2"

So "\\&" as a replacement string means perform the replacement, but use the same text as what you are replacing.

If you want to replace something with \&, you need to double the backslashes:

puts "test ?".sub("?", "\\\\&")
# output: test \&

If you want to replace something with &, use no backslashes:

puts "test ?".sub("?", '&')
# output: test &

Python does not handle \& specially, which is why the behavior is different.

Updated by thiaguerd (thiago feitosa) about 1 year ago

include ERB::Util
def r
  puts "Enter original"
  a = gets.chomp
  puts "Enter a pattern to replace"
  b = gets.chomp
  puts "Enter a replacement"
  c = gets.chomp
  puts "a: #{a}"
  puts "b: #{b}"
  puts "c: #{c}"
  c = html_escape c 
  puts "c escaped: #{c}"
  puts "final: #{a.gsub(b,c)}"
end

running

>> r
Enter original
my text and my source: ###
Enter a pattern to replace
###
Enter a replacement
xml.scan(/\<tag>[\s\S]*?\<\/tag\>/)
a: my text and my source: ###
b: ###
c: xml.scan(/\<tag>[\s\S]*?\<\/tag\>/)
c escaped: xml.scan(/\&lt;tag&gt;[\s\S]*?\&lt;\/tag\&gt;/)
final: my text and my source: xml.scan(/###lt;tag&gt;[\s\S]*?###lt;\/tag###gt;/)
=> nil
>> 
# should not be: "final: my text and my source: xml.scan(/\&lt;tag&gt;[\s\S]*?\&lt;\/tag\&gt;/)" ??
#8

Updated by thiaguerd (thiago feitosa) about 1 year ago

  • Status changed from Rejected to Open

Updated by thiaguerd (thiago feitosa) about 1 year ago

on Python 3.7.1

import html
def r():
  a = input('Enter original:')
  b = input('Enter a pattern to replace:')
  c = input('Enter a replacement:')
  print(f"a: {a}")
  print(f"b: {b}")
  print(f"c: {c}")
  c = html.escape(c)
  print(f"c escaped: {c}")
  print(f"final: {a.replace(b,c)}")

running

>>> r()
Enter original:my text and my source: ###
Enter a pattern to replace:###
Enter a replacement:xml.scan(/\<tag>[\s\S]*?\<\/tag\>/)
a: my text and my source: ###
b: ###
c: xml.scan(/\<tag>[\s\S]*?\<\/tag\>/)
c escaped: xml.scan(/\&lt;tag&gt;[\s\S]*?\&lt;\/tag\&gt;/)
final: my text and my source: xml.scan(/\&lt;tag&gt;[\s\S]*?\&lt;\/tag\&gt;/)
>>>
# here works ...

Updated by alanwu (Alan Wu) about 1 year ago

gsub(pattern, replacement) always interprets replacement as a regex replacement directive.
You can use the block form to substitute verbatim:

a = "test ?"
b = "?"
c = "\\&"
a.gsub(b) { c } #=> "test \\&"

Updated by thiaguerd (thiago feitosa) about 1 year ago

  • Backport deleted (2.5: UNKNOWN, 2.6: UNKNOWN)
  • ruby -v deleted (2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux])
  • Tracker changed from Bug to Misc

alanwu (Alan Wu) wrote:

gsub(pattern, replacement) always interprets replacement as a regex replacement directive.
You can use the block form to substitute verbatim:

a = "test ?"
b = "?"
c = "\\&"
a.gsub(b) { c } #=> "test \\&"

Nice, tank you.

#12

Updated by matz (Yukihiro Matsumoto) about 1 year ago

  • Status changed from Open to Closed

Also available in: Atom PDF