Why does Regexp::escape backslash spaces?!?

$ 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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/.

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++ = '\\';
            *t++ = ' ';
            continue;

Kirk Haines

···

On Tue, Jan 26, 2010 at 1:12 PM, Marnen Laibow-Koser <marnen@marnen.org> wrote:

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

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 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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.