On 2/20/08, Ryan Davis <ryand-ruby / zenspider.com> wrote:
>
>  On Feb 18, 2008, at 23:11 , Tanaka Akira wrote:
>
>  > More problems:
>  >
>  > % ./miniruby -e 'p %r(\))'
>  > -e:1: unmatched ): /)/
>  > % ./miniruby -e 'p %r|\|| =~ "a"'
>  > 0
>  > % ./miniruby -e 'p %r?\??'
>  > -e:1: invalid regular expression; there's no previous pattern, to
>  > which '?' would define cardinality at 1: /?/
>>
>
> thanks... I'll look into these problems and try to find a compromise.

I think that Akira-san brought up a subtle point, which might have
been glossed over:

> Note that %r%/%.source is "/".  However p %r%/% prints
> /\//.  The backslash in /\// is added by Regexp#inspect.  It
> is safe because a slash is not a meta character for regexp
> engine.

I think that the problem here, if there is one is not with the parsing
but with Regexp#inspect.  It seems akin to the recurring question
about how to get rid of "extra" backslashes that Ruby "adds" to
strings but which really aren't there:

irb(main):001:0> s = "blah\\blah"
=> "blah\\blah"
irb(main):002:0> puts s
blah\blah
=> nil

irb(main):004:0> s2 = %[abc\\def]
=> "abc\\def"
irb(main):005:0> puts s2
abc\def

Since backslash escaping a character which doesn't NEED to be escaped
doesn't really change anything, I'm pretty sure that

/blah%blah/
and
/blah\%blah/

are equivalent regular expressions.


But then I could be wrong!

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/