s/ ( (?: # non capturing parens ^|%> # start of string or %> ) .* ) ' (.* (?:<%|$) ) /$1 SOMESTRING $2/gsx # add s and x modifiers