in reply to Maximal match in a recursive regex

Ah I see. The key is to change the capturing group to match one or more times instead of just once. It just becomes )+ from ).

Added: I goofed. That is *part* of the key. The above change has a maximal match but loses the contents of the non-innermost matches. Here's a version that *works*

$re = qr/ \[ # Opening bracket ((?: # Capture the contents [^][]+ # Body of a link | (??{$re}) # Or recurse )+) # and allow repeats internally \] # Closing bracket /x;

Noted: for a brief period there was also some pushing into @f. That shouldn't have been posted so I removed it.

Replies are listed 'Best First'.
Re: Re: Maximal match in a recursive regex
by Mr. Muskrat (Canon) on Jun 26, 2003 at 17:35 UTC

    This is how I fixed it.

    my $re; $re = qr/ \[ # Opening bracket ( # Capture the contents [^][]+ # Body of a link | (??{$re}) # Or recurse )+ # added per diotalevi's instructions \] # Closing bracket /x; $k = "a[b[c[d]]"; $k =~ s/($re)/<$1>/g; # I added the ()'s :) print $k;

    Bah! It captures the outer square brackets too :(