Alternate solutions. The simplest is to use a minimal match: m/<%.*?%>/s. However you can get by with basic features with something like: m/<%[^%]*(%+[^%>][^%]*)*%+>/. That should, possibly with appropriate syntax adjustments, work in any RE engine worthy of the name.