On Nov 21, 9:11 am, Wayne Magor <wema... / hotmail.com> wrote:
> Noah Easterly wrote:
> > On Nov 18, 8:50 pm, Wayne Magor <Wayne.Ma... / gmail.com> wrote:
> >> I understand regular expressions, but can someone please explain this:
>
> >> re = %r/((?<pg>\((?:\\[()]|[^()]|\g<pg>)*\)))/
> snip
> > (?: ... | ... | ... )*
> > -- non-capturing group of 3 alternatives, repeated 0 or more times
> > \\[()]
> > -- escaped literal parens
> > [^()]
> > -- anything except parens
> > \g<pg>
> > -- match the pg-named pattern here
>
> Ok, so there are 3 alternatives in the non-capturing group:
>
> 1.  An open or close parenthesis
correction.  As Eric said above, an escaped (read, with leading
backslash) parenthesis.

> 2.  Any character except a paren
yup.
> 3.  A pattern that starts with an open paren
AND ends in a close paren, and contains only, non-parens, escaped
parens, and balanced pairs of parens.
>
> Am I the only one that finds this strange?
Doubtful :).  You may be one of the ones to which this is new, though.

I find it strange that only recognize parenthesis escapes, and not
escaped backslashes.  So you can do something like:
( \( )
and match correctly, but there's no way to do a balanced pair of
parentheses containing just a backslash:
(\) -- no
(\\) -- no
(\\\) -- no
(\ ) -- matches, but has an extra space.

I would have replaced '\\[()]' by '\\[()\\]' so that '(\\)' would
match.
>
> --
> Posted viahttp://www.ruby-forum.com/.