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