Q:

Getting the outer most delimiters in Regular Expressions

Is there a way using regular expressions to get the text in between outermost delimiters?  I have a string here and want to get the text in between the outermost {%%% and %%%} delimiters:
Hello {%%%=Select(DepartmentID,1,{%%%=if(Gender="M","Mr.","Ms.")%%%}%%%} {%LastName%}

The text I want to get is:
=Select(DepartmentID,1,{%%%=if(Gender="M","Mr.","Ms.")%%%}

What would the the regular expression for that?  I know the text inside does not make much sense, this is just an example.

A:

This pattern will do a positive lookahead:
[^%=]*.{%%%(.+)%%%}.+[^%}]*

capture group:
$1

example: http://regex101.com/r/eG4fV9
EDIT: It seems some people enjoy coming along after an answer was chosen as correct then adding possible scenarios where it won't work. That's fine, however, depending on the circumstances in which something is used can make anything incorrect.
original answer:
(?<={%%%=).+(?=}%%%)[^%]

optional:
[^%=]*.{%%%=(.+)%%%}.+[^%}]*

This will retain the = sign in the matches.

