How about you use the actual Razor template engine to do the job for you.
Make sure you have
<pre lang="c#">using System.Web.Razor;
and then something like that
RazorEngineHost _host = new RazorEngineHost(new CSharpRazorCodeLanguage());
RazorTemplateEngine templateEngine = new RazorTemplateEngine(_host);
var parserResult = templateEngine.ParseTemplate(new StringReader(<YOU CONTENT HERE>));
Cheers,
Andrzej