in reply to A NOT in regular expressions

Being lazy I would suggest using Regexp::Common::balanced.

Taking into account all the problems with writing a regexp for balanced delimiters I wonder how it can work in the generall case, but I hope it works.

Update: It's not really equivalent to the other proposed solutions. But it has a clear semantics that shoul be applicable in many cases.

Here is an example:

use Regexp::Common qw /balanced/; $r = qr/$RE{balanced}{-begin => "<%"}{-end => "%>"}/; @list = ("aa<% <% %>bb", "aa<% %> <% %>bb", "aa<% <% %> %>bb", "aa<% > +% %>bb"); for (@list){ /($r)/; print "$` \t| $1 \t| $'\n"; }
and the output:
aa<% | <% %> | bb aa | <% %> | <% %>bb aa | <% <% %> %> | bb aa | <% >% %> | bb