Question about strings

W

William Carpin

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?
 
G

Grant Hollingworth

* William Carpin said:
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!"
 
C

Chris Gernon

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

=> "I am Will\\'s string!"

Using a block for the substitution appears to work:
=> "This is Will\\'s string!"
 
A

ara.t.howard

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?

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


-a
 
S

Stefan Mahlitz

William said:
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
 
R

Robert Klemme

William said:
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?
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


2. 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

3. 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top