in reply to Regex To remove text between parentheses

Here's a grunky version.
$re = qr{ \( (?{ local $N = 1 }) (?: (?(?{ !$N })(?!)) (?: \( (?{ local $N = $N + 1 }) | \) (?{ local $N = $N - 1 }) | [^()]+ ) )+ (?(?{ $N })(?!)) # fixed, thanks to Hofmator }x; $text =~ s/$re//g;


japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Regex To remove text between parentheses
by Hofmator (Curate) on Jul 10, 2001 at 19:37 UTC

    Or another version for this nested parens matching from the camel (3rd edition) which is a little bit easier to understand IMHO. But I'd guess that the recursion makes it slower

    $re = qr{ \( (?: (?> [^()]+ ) # Non-parens without backtracking | (??{ $re }) # Group with matching parens )* \) }x; $text =~ s/$re//g;

    -- Hofmator

Re: Re: Regex To remove text between parentheses
by sierrathedog04 (Hermit) on Jul 10, 2001 at 21:33 UTC
    Someone mentioned the other day that repeated nested recursions of the '?' produces hard to read code. Certainly it works but my preference would be for some other way.

    I guess that is what you meant by 'grunky'.