Question about strings

Discussion in 'Ruby' started by William Carpin, Oct 26, 2006.

  1. 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?

    --
    Posted via http://www.ruby-forum.com/.
     
    William Carpin, Oct 26, 2006
    #1
    1. Advertising

  2. * William Carpin <> [2006-10-26 10:25]:
    >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, Oct 26, 2006
    #2
    1. Advertising

  3. William Carpin

    Chris Gernon Guest

    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!"

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gernon, Oct 26, 2006
    #3
  4. William Carpin

    Guest

    On Fri, 27 Oct 2006, William Carpin 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.
    >
    > 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
    --
    my religion is very simple. my religion is kindness. -- the dalai lama
     
    , Oct 26, 2006
    #4
  5. William Carpin 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
     
    Stefan Mahlitz, Oct 26, 2006
    #5
  6. William Carpin 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.
    >
    > 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
     
    Robert Klemme, Oct 26, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    726
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    394
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    514
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    581
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    801
    Malcolm
    Jun 24, 2006
Loading...

Share This Page