Backslashes

Discussion in 'Ruby' started by Han Holl, Sep 29, 2005.

  1. Han Holl

    Han Holl Guest

    ------=_Part_2456_4241297.1128002290320
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Hi,

    Why does
    '\\'.gsub('\\','\\\\').size
    produce 1 ?

    Is this a bug, or should it be specially documented ?

    Cheers,

    Han Holl

    ------=_Part_2456_4241297.1128002290320--
     
    Han Holl, Sep 29, 2005
    #1
    1. Advertising

  2. Han Holl

    ts Guest

    >>>>> "H" == Han Holl <> writes:

    H> Is this a bug, or should it be specially documented ?

    Well, it's documented but in the FAQ


    Regexp.quote('\\') escapes a backslash.

    It gets trickier if you're using sub and gsub, Say you write gsub(/\\/,
    '\\\\'), hoping to replace each backslash with two. The second argument is
    converted to '\\' in syntax analysis. When the substitution occurs, the
    regular expression engine converts this to '\', so the net effect is to
    replace each single backslash with another single backslash. You need to
    write gsub(/\\/, '\\\\\\')!


    Guy Decoux
     
    ts, Sep 29, 2005
    #2
    1. Advertising

  3. On 9/29/05, Han Holl <> wrote:
    > Hi,
    >
    > Why does
    > '\\'.gsub('\\','\\\\').size
    > produce 1 ?
    >
    > Is this a bug, or should it be specially documented ?


    It's not a bug, and it's relatively well known. I recommend:

    '\\'.gsub(/\\/) { "\\\\" }.size

    The *trick* is that strings consider \ special, so to use special
    regexp values (like \1), you need \\1. So to get \\, you actually need
    \\\\\\\\ (yes, eight of them). The first round of interpolation
    (string) will result in \\\\; the second round of interpolation
    (regexp substitution values) will result in \\.

    -austin
    --
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Sep 29, 2005
    #3
  4. On Sep 29, 2005, at 7:58 AM, Han Holl wrote:
    > Why does
    > '\\'.gsub('\\','\\\\').size
    > produce 1 ?
    >
    > Is this a bug, or should it be specially documented ?


    See "Explanation for the multitude of backslashes" on http://
    rubygarden.org/ruby?RegexpCookbook
     
    Gavin Kistner, Sep 29, 2005
    #4
  5. Han Holl

    Han Holl Guest

    ------=_Part_2808_26441850.1128005622184
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 9/29/05, Gavin Kistner <> wrote:
    >
    > On Sep 29, 2005, at 7:58 AM, Han Holl wrote:
    > > Why does
    > > '\\'.gsub('\\','\\\\').size
    > > produce 1 ?
    > >
    > > Is this a bug, or should it be specially documented ?

    >
    > See "Explanation for the multitude of backslashes" on http://
    > rubygarden.org/ruby?RegexpCookbook<http://rubygarden.org/ruby?RegexpCookb=

    ook>
    >
    > Thanks to you all.

    Pickaxe2 seems to be silent about this. Maybe pickaxe3 ?

    Cheers,

    Han Holl

    ------=_Part_2808_26441850.1128005622184--
     
    Han Holl, Sep 29, 2005
    #5
  6. Han Holl wrote:
    > On 9/29/05, Gavin Kistner <> wrote:
    >
    >>On Sep 29, 2005, at 7:58 AM, Han Holl wrote:
    >>
    >>>Why does
    >>>'\\'.gsub('\\','\\\\').size
    >>>produce 1 ?
    >>>
    >>>Is this a bug, or should it be specially documented ?

    >>
    >>See "Explanation for the multitude of backslashes" on http://
    >>rubygarden.org/ruby?RegexpCookbook<http://rubygarden.org/ruby?RegexpCookbook>
    >>
    >>Thanks to you all.

    >
    > Pickaxe2 seems to be silent about this. Maybe pickaxe3 ?
    >
    > Cheers,
    >
    > Han Holl
    >

    You cannot have one book cover EVERY aspect of a subject. People will
    hate when it grows to several volumes. That's where online resouces come
    in handy.

    Gennady.
     
    Gennady Bystritksy, Sep 29, 2005
    #6
  7. On Sep 29, 2005, at 8:53 AM, Han Holl wrote:
    > Pickaxe2 seems to be silent about this. Maybe pickaxe3 ?



    Pickaxe 2 very clearly discusses this on page 75.
     
    Gavin Kistner, Sep 30, 2005
    #7
    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. Replies:
    1
    Views:
    14,880
    Roedy Green
    Sep 5, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,482
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?=

    GridView and Backslashes

    =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?=, May 2, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?=
    May 2, 2006
  4. Mikheil

    Double BackSlashes

    Mikheil, Aug 14, 2003, in forum: C++
    Replies:
    6
    Views:
    549
    Mikheil
    Aug 15, 2003
  5. Replies:
    6
    Views:
    384
Loading...

Share This Page