in reply to capturing matching parenthesis

I'd say not to use a regexp. Just walk over the string byte by byte. Keep a $parenthesis variable. ++ it when you encounter a (, -- it when you encounter a ). Stop when $parenthesis has been >0 and now =0 again.

Good luck.

Replies are listed 'Best First'.
Re: Re: capturing matching parenthesis
by davido (Cardinal) on Apr 15, 2004 at 21:13 UTC
    Fine, unless you care about the possibility of escaped parens. For example, '( ( \( two ) )' would fail with your method unless you specifically were watching for that sort of thing with additional logic.


    Dave

      Hmm, I was indeed not taking care of backwhacks.

      But then, the method would not change that much. Also keep track of another variable, let's call it $escape. Set it to 1 if you find a \. Then, in the next iteration of the loop, if that character is a ( or ) and $escape != 0, ignore it. If it is a backslash, ignore it too. Then set $escape back to 0.
        You'll also need a special case for '( \\( ( three ) ) )'. In this case, there are three sets of parens, because the backslash itself is escaped.

        The solutions quickly become ugly, complex, and error-prone, which is why the tried and proven module Text::Balanced is a good idea.

        Update: Ok, MUBA's approach handles the escaped-backslash case too.

        My point is that if the idea of Perl is to make easy things simple and difficult things possible, I'd favor an approach that takes it a step further by making a difficult thing easy for me.


        Dave