in reply to balanced parens regexp hangs - solved

Your code does not hang on my machine, with perl-5.10.0.
(?:[^{}]+?|\{(??{$parens})\})*

Such a regex with nested quantifiers is potentially dangerous, because the outer quantifier can force the inner quantifier to backtrack. Use a non-backtracking group (?>...)* instead. (Oh, and why are you using non-greed +? on the [^{}] group? I see no reason for that).

Replies are listed 'Best First'.
Re^2: balanced parens regexp hangs
by grizzley (Chaplain) on Mar 27, 2009 at 08:32 UTC

    I run it on WinXP with ActiveState Perl v5.10.0. (build 1004).

    Localizing helped solving my problem, but I removed non-greed and tried to use (?>...)* but I must admit I don't fully understand documentation of this construct yet. Did you think about something like this?

    local $parens = qr{(?:(?>[^{}]*)|\{(??{$parens})\})*}x;
        I agree. Thanks again!