$re = qr /(?:[\s\n]*|[\s]*%[^\n]*\n)* (? # start capture buffer 1? \{ # match an opening paren ( # capture buffer 2 (?: # match one of: (?> # don't backtrack over the inside of this group [^{}]+ # one or more ) # end non backtracking group | # ... or ... (?&first) # recurse to opening 1 and try it again &first )*? # 0 or more times. ) # end of buffer 2 \} # match a closing paren ) # end capture buffer one /x; $s =~ /\n[^%]*\\Befehl$re/g;