http://www.perlmonks.org?node_id=269318


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.