Used 2 regular expressions. 1st to match the general form, 2nd to deal with the inner plumbing.
For the XML encoding I used an obscure little method found in System.Security: SecurityElement.Escape Method. I fully qualified it in the code below for emphasis. Another option would be using the HttpUtility.HtmlEncode method but that may involve a reference to System.Web depending on where you're using this.
string[] inputs = { @"[% 'test' <mtd:ddl id=""asdf"" runat=""server""/> & <%= Integer.MaxValue %> %]",
@"[% 'test' <mtd:ddl id=""asdf"" runat=""server""/> & <%=Integer.MaxValue %> %]",
@"[% 'test' <mtd:ddl id=""asdf"" runat=""server""/> & <%# Integer.MaxValue%> %]",
@"[% 'test' <mtd:ddl id=""asdf"" runat=""server""/> & <%#Integer.MaxValue%> %]",
};
string pattern = @"(?<open>\[%)(?<content>.*?)(?<close>%])";
string expressionPattern = @"(?<content>.*?)(?<tag><%(?:[=#]))\s*(?<expression>.*?)\s*%>";
foreach (string input in inputs)
{
string result = Regex.Replace(input, pattern, m =>
m.Groups["open"].Value +
Regex.Replace(m.Groups["content"].Value, expressionPattern,
expressionMatch =>
System.Security.SecurityElement.Escape(expressionMatch.Groups["content"].Value) +
expressionMatch.Groups["tag"].Value + " " +
expressionMatch.Groups["expression"].Value +
" %>"
) +
m.Groups["close"].Value
);
Console.WriteLine("Before: {0}", input);
Console.WriteLine("After: {0}", result);
}
Results:
Before: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%= Integer.MaxValue %> %]
After: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%= Integer.MaxValue %> %]
Before: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%=Integer.MaxValue %> %]
After: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%= Integer.MaxValue %> %]
Before: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%# Integer.MaxValue%> %]
After: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%# Integer.MaxValue %> %]
Before: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%#Integer.MaxValue%> %]
After: [% 'test' <mtd:ddl id="asdf" runat="server"/> & <%# Integer.MaxValue %> %]
EDIT: if you don't care to preserve the opening/closing [%%] in the final result then change the pattern to:
string pattern = @"\[%(?<content>.*?)%]";
Then be sure to remove references to m.Groups["open"].Value
and m.Groups["close"].Value
.