in reply to regex parsing brainteaser

You really need a parser. Using extended regexp features, you can write a parser in a regexp.
use strict; use warnings; my $text = <<'__EOI__'; <%def .errors> missing_name: You must provide your name. missing_email: You must provide your email address. </%def> __EOI__ # Perl code in regexps close over lexicals when # the regexp is compiled. It's best to avoid # using lexical variables declared outside the # regexp in Perl code in regexps. local our %errors; $text =~ / (?{ +{} }) <%def\ \.errors> \n (?: (\w+): \s* (.*) \n (?{ +{ %{$^R}, $1 => $2 } }) )* <\/%def> \n (?{ %errors = %{$^R} }) /x or die("Bad text\n"); for (keys %errors) { print("$_ => $errors{$_}\n"); }

5.10 has features that make this even easier, but I haven't taken the time to look at them yet.