Why does Regexp::escape backslash spaces?!?

Discussion in 'Ruby' started by Marnen Laibow-Koser, Jan 26, 2010.

  1. $ ruby -v
    ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
    $ irb
    irb(main):001:0> Regexp::escape('a b')
    => "a\\ b"

    Why? This doesn't seem to make any sense. Is it a bug?

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Jan 26, 2010
    #1
    1. Advertising

  2. Marnen Laibow-Koser

    Kirk Haines Guest

    On Tue, Jan 26, 2010 at 1:12 PM, Marnen Laibow-Koser <> wr=
    ote:
    > $ ruby -v
    > ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
    > $ irb
    > irb(main):001:0> Regexp::escape('a b')
    > =3D> "a\\ b"
    >
    > Why? =A0This doesn't seem to make any sense. =A0Is it a bug?


    It is intentional. In re.c:rb_reg_quote()

    switch (c) {
    case '[': case ']': case '{': case '}':
    case '(': case ')': case '|': case '-':
    case '*': case '.': case '\\':
    case '?': case '+': case '^': case '$':
    case ' ': case '#':
    case '\t': case '\f': case '\n': case '\r':
    goto meta_found;
    }


    And then down in meta_found:

    case ' ':
    *t++ =3D '\\';
    *t++ =3D ' ';
    continue;


    Kirk Haines
    Kirk Haines, Jan 26, 2010
    #2
    1. Advertising

  3. Marnen Laibow-Koser wrote:
    > irb(main):001:0> Regexp::escape('a b')
    > => "a\\ b"
    >
    > Why? This doesn't seem to make any sense. Is it a bug?



    Maybe spaces are escaped so the resulting string be compatible with an
    "extended" pattern, where non-escaped spaces are ignored:

    / (\w+) \s (\w+) /x

    is the same as:

    /(\w+)\s(\w+)/
    --
    Posted via http://www.ruby-forum.com/.
    Albert Schlef, Jan 26, 2010
    #3
  4. Albert Schlef wrote:
    > Marnen Laibow-Koser wrote:
    >> irb(main):001:0> Regexp::escape('a b')
    >> => "a\\ b"
    >>
    >> Why? This doesn't seem to make any sense. Is it a bug?

    >
    >
    > Maybe spaces are escaped so the resulting string be compatible with an
    > "extended" pattern, where non-escaped spaces are ignored:
    >
    > / (\w+) \s (\w+) /x
    >
    > is the same as:
    >
    > /(\w+)\s(\w+)/


    Good point. And anyway, it causes no problems: when I first posted, I
    thought it did, but those problems actually came from elsewhere.

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Jan 26, 2010
    #4
    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. Vincent Texier
    Replies:
    3
    Views:
    2,234
    Vincent Texier
    Jul 1, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,656
    Smokey Grindel
    Dec 2, 2006
  3. vlsidesign
    Replies:
    5
    Views:
    602
    Keith Thompson
    Sep 23, 2007
  4. Sisilla
    Replies:
    4
    Views:
    723
    Daniel Pitts
    Nov 8, 2007
  5. Terry Asher

    Backslash does NOT escape Apostrophe

    Terry Asher, Sep 3, 2003, in forum: Javascript
    Replies:
    3
    Views:
    140
    Terry Asher
    Sep 3, 2003
Loading...

Share This Page