Question about strings

Okay, given the following string:

“This is Will’s string!”

what I’m trying to do is replace the single-tick “’” character with a
backslash/single-tick “’”, so I can use use that string in a sql query.

To do this in Perl, you’d do the following:

myString =~ s/’//g;

…which doesn’t work in Ruby.

I also tried using the gsub function, with “\’” passed in as the
second parameter, and it just started outputting some sort sort of
goofiness with characters replaced by sub-elements of the string for
which I was calling the function on and the like.

I scoured Google for awhile but didn’t find anything. Any ideas?

I also tried using the gsub function, with “\'” passed in as the
second parameter, and it just started outputting some sort sort of
goofiness with characters replaced by sub-elements of the string for
which I was calling the function on and the like.

Your replacement string ends up as ', which is the equivalent of Perl’s
$’ (the substring from the match to the end).

The best solution I could find was to match the space before the
quotation mark.

“I am Will’s string!”.gsub(/(?=‘)/,’\')
=> “I am Will\'s string!”

Grant Hollingworth wrote:

The best solution I could find was to match the space before the
quotation mark.

“I am Will’s string!”.gsub(/(?=’)/,’\’)
=> “I am Will\'s string!”

Using a block for the substitution appears to work:

“This is Will’s string!”.gsub(/(’)/) {|m| ‘\’ + m}
=> “This is Will\'s string!”

On Fri, 27 Oct 2006, William C. wrote:

…which doesn’t work in Ruby.

I also tried using the gsub function, with “\’” passed in as the
second parameter, and it just started outputting some sort sort of
goofiness with characters replaced by sub-elements of the string for
which I was calling the function on and the like.

I scoured Google for awhile but didn’t find anything. Any ideas?

irb(main):001:0> “This is Will’s string!”.gsub(/’/){ %q(’) }
=> “This is Will\'s string!”

-a

William C. wrote:

/ …

I scoured Google for awhile but didn’t find anything. Any ideas?

#!/usr/bin/ruby -w

a = “This is Will’s string!”

b = a.gsub(%r{’},"\\’")

puts a,b

Output:

This is Will’s string!
This is Will’s string!

William C. wrote:

Okay, given the following string:

“This is Will’s string!”

what I’m trying to do is replace the single-tick “’” character with a
backslash/single-tick “’”, so I can use use that string in a sql query.

Can’t you use the SQL-engine escape-method, if it exists?

Stefan

William C. wrote:

…which doesn’t work in Ruby.

I also tried using the gsub function, with “\’” passed in as the
second parameter, and it just started outputting some sort sort of
goofiness with characters replaced by sub-elements of the string for
which I was calling the function on and the like.

I scoured Google for awhile but didn’t find anything. Any ideas?

This comes up from time to time. The crucial bit is to understand the
levels of quoting / interpretation involved. Here is a general escaping
piece:

irb(main):017:0> puts “This is Will’s string!”.gsub(/’/, ‘\\\&’)
This is Will’s string!
=> nil

Explanation:

  1. level is the string interpretation. You need to escape a backslash
    to have him literally in a string:

irb(main):019:0> puts ‘\’

=> nil

  1. the RX engine uses backslash as escape character as well, for example
    to reference groups with \1

So you need four (4) backslashes to make the RX engine insert a single
backslash:

irb(main):020:0> puts “This is Will’s string!”.gsub(/’/, ‘\\’)
This is Will\s string!
=> nil

  1. the RX engine uses & as a placeholder for the match:

irb(main):022:0> puts “This is Will’s string!”.gsub(/’/, ‘<\&>’)
This is Will<’>s string!
=> nil

Combined makes 3 * 2 = 6 backslashes.

Bonus reply: although the block form is easier on the eye it is actually
less performant because the block has to be evaluated for every match.
This is rather intended for replacements that are not static, i.e. where
the replacement depends in a way on the match that cannot be expressed
with the usual mechanisms in the replacement string. For example:

irb(main):023:0> c=0; puts “This is Will’s string!”.gsub(/i/) { c+= 1 }
Th1s 2s W3ll’s str4ng!
=> nil

Kind regards

robert