in reply to Parsing nested parentheses

Simple? Perhaps. For the moment have a gander at Maximal match in a recursive regex which is the bare minimum required if your going to merely capture maximum. My first thought is to follow the $re check with a code block that can check the just-captured result for the contents of your search.

for ( A .. E ) { print "$_: "; print '((A, B), C, (D, E))' =~ match( $_ ); # Returns '(D, E)' print "\n"; } sub match { my $search = shift; my $re; eval qq[ \$re = qr/ \\( # Opening parenthese ((?: # Capture the contents [^()]+ # Body of a link | (??{\$re}) # Or recurse )+) # and allow repeats internally \\) # Closing parenthese # Test that the just-closed capture block contains $search +. (?(?{ -1 != index \$^N, \$search })(?=)|(?!)) /x; ] or die $@; return $re; }

Replies are listed 'Best First'.
Re: Re: Parsing nested parentheses
by Anonymous Monk on Nov 18, 2003 at 17:57 UTC

    That doesn't give any result when 'C' is the argument.

      Oh too bad. It "should". There's also some funny action with stuff being cached. For kicks, try a for ( A .. E ) { ... =~ match( $_ ) } and you'll notice that the expression never shifts off of its initial search.