在C#ASP.NET中使用正则表达式替换函数

在C#ASP.NET中使用正则表达式替换函数,c#,asp.net,regex,C#,Asp.net,Regex,如何解决下面的问题 我正在创建一个简单的内容管理系统,其中有一个带有特定标记的HTML模板,该标记指示内容应该在哪里: <html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html> 我在这里开始编写代码。但是我在使用Regex

如何解决下面的问题

我正在创建一个简单的内容管理系统,其中有一个带有特定标记的HTML模板,该标记指示内容应该在哪里:

<html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html>
我在这里开始编写代码。但是我在使用Regex Replace函数时遇到了问题,该函数应该位于For/Each…的最底部

    //Html Template
    string html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";        

    //Regions that need to be put in the Html Template
    string regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";

    //Create a Regex to only extract what's between the 'Body' tag
    Regex oRegex = new Regex("<body.*?>(.*?)</body>", RegexOptions.Multiline);

    //Get only the 'Body' of the html template
    string body = oRegex.Match(html).Groups[1].Value.ToString();

    // Regex to find sections inside the 'Body' that need replacing with what's in the string 'regions'
    Regex oRegex1 = new Regex("<!-- #Editable \"(.*?)\"[^>]*>",RegexOptions.Multiline);
    MatchCollection matches = oRegex1.Matches(body);

    // Locate section titles i.e. Body1, Extra
    foreach (Match match in matches)
    {
        string title = oRegex1.Match(match.ToString()).Groups[1].ToString();
        Regex oRegex2 = new Regex("<!-- #BeginEditable \"" + title + "\"[^>]*>(.*?)<!-- #EndEditable [^>]*>", RegexOptions.Multiline);
        //
        //
        // Replace the 'Body' sections with whats in the 'regions' string cross referencing the titles i.e. Body1, Extra
        //
        //
        //
    }
//Html模板
字符串html=“等”

”; //需要放入Html模板中的区域 string regions=“这是测试文本这是更多测试文本”; //创建正则表达式以仅提取“Body”标记之间的内容 Regex-oRegex=new Regex(“(*?”,RegexOptions.Multiline); //仅获取html模板的“正文” string body=oRegex.Match(html).Groups[1].Value.ToString(); //Regex查找“Body”中需要替换为字符串“regions”中的部分
Regex oRegex1=new Regex(“最好先使用来处理这个问题,然后再使用Regex。它可以将Html解析为DOM结构中类似XML的树,使用此包可以更轻松地处理此问题

编辑:

string sReg = @"<body.*?>((?<Region>\<\!\-\-\s+\#Editable\s?\\$(?<editable>.+)\\$\s?\-\-\>[^\>]).*?)"; string sNewReg = sReg1.Replace('$', '\"'); System.Diagnostics.Debug.WriteLine(string.Format("Regex: {0}", sNewReg)) Regex MyRegex = new Regex(sNewReg, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled ); string sMg = "<html><head></head><body><!-- #Editable \\\"Body1\\\" --><p>etc etc</p><!-- #Editable \\\"Extra\\\" --></body></html>"; Match m = MyRegex.Match(sMg); if (m.Success) { System.Diagnostics.Debug.WriteLine(string.Format("{0}", m.Groups["editable"].Value)); } 字符串sReg=@“((?\[^\>]).*?”; string sNewReg=sReg1.Replace(“$”,“\”);System.Diagnostics.Debug.WriteLine(string.Format(“Regex:{0}”,sNewReg)) 正则表达式MyRegex=新正则表达式(sNewReg, RegexOptions.IgnoreCase |RegexOptions.CultureInvariant |RegexOptions.IgnorePatternWhitespace |RegexOptions.Compiled ); 字符串sMg=“etc-etc

”; 匹配m=MyRegex.Match(sMg); 如果(m.成功) { System.Diagnostics.Debug.WriteLine(string.Format({0}),m.Groups[“editable”].Value)); } 请注意,我必须在适当的位置使用美元符号来防止转义,并在运行时将其替换为双引号

希望这有帮助, 顺致敬意,
Tom。

我建议对这类东西使用类似的模板引擎。

没有针对性能(或任何其他方面)进行优化,但它很简单且有效:

var html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";
var regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";
var regionRegex = new Regex(@"<!-- #BeginEditable ""(?<Name>\w+)"" -->(?<Content>.*?)<!-- #EndEditable -->", RegexOptions.Multiline);
var regionMatches = regionRegex.Matches(regions);

foreach (Match regionMatch in regionMatches)
{
    var regionName = regionMatch.Groups["Name"].Value;
    var regionContent = regionMatch.Groups["Content"].Value;
    html = html.Replace(string.Format(@"<!-- #Editable ""{0}"" -->", regionName), regionContent);
}
var html=“etc

”; var regions=“这是测试文本这是更多测试文本”; var regionRegex=newregex(@“(?*?”),RegexOptions.Multiline; var regionMatches=regionRegex.Matches(区域); foreach(区域匹配中的匹配区域匹配) { var regionName=regionMatch.Groups[“Name”].Value; var regionContent=regionMatch.Groups[“Content”].Value; html=html.Replace(string.Format(@“”,regionName),regionContent); }
使用MatchEvaluator作为匿名代理,您的代码看起来像

string html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";
string regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";

Regex oRegex1 = new Regex("<!-- #Editable \"(.*?)\"[^>]*>", RegexOptions.Multiline);

html = oRegex1.Replace(html, delegate(Match m) {
    string title = m.Groups[1].Value;
    Regex oRegex2 = new Regex("<!-- #BeginEditable \"" + title + "\"[^>]*>(.*?)<!-- #EndEditable [^>]*>", RegexOptions.Multiline);
    return oRegex2.Match(regions).Groups[1].Value;
});
string html=“etc

”; string regions=“这是测试文本这是更多测试文本”;
Regex oRegex1=new Regex(“我有时间限制,离成功只有一行代码!”:(我害怕不止一行,我有时间限制,离成功只有一行代码!);(太好了,你让我感到羞愧了!顺便说一句,你应该将
regionRegex
提升到私有静态字段,并附加
RegexOptions.Compiled
标志。还可以使用
StringBuilder
在循环内部进行替换。感谢大家……注意,我喜欢这种简单性。 string sReg = @"<body.*?>((?<Region>\<\!\-\-\s+\#Editable\s?\\$(?<editable>.+)\\$\s?\-\-\>[^\>]).*?)"; string sNewReg = sReg1.Replace('$', '\"'); System.Diagnostics.Debug.WriteLine(string.Format("Regex: {0}", sNewReg)) Regex MyRegex = new Regex(sNewReg, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled ); string sMg = "<html><head></head><body><!-- #Editable \\\"Body1\\\" --><p>etc etc</p><!-- #Editable \\\"Extra\\\" --></body></html>"; Match m = MyRegex.Match(sMg); if (m.Success) { System.Diagnostics.Debug.WriteLine(string.Format("{0}", m.Groups["editable"].Value)); }
var html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";
var regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";
var regionRegex = new Regex(@"<!-- #BeginEditable ""(?<Name>\w+)"" -->(?<Content>.*?)<!-- #EndEditable -->", RegexOptions.Multiline);
var regionMatches = regionRegex.Matches(regions);

foreach (Match regionMatch in regionMatches)
{
    var regionName = regionMatch.Groups["Name"].Value;
    var regionContent = regionMatch.Groups["Content"].Value;
    html = html.Replace(string.Format(@"<!-- #Editable ""{0}"" -->", regionName), regionContent);
}
string html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";
string regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";

Regex oRegex1 = new Regex("<!-- #Editable \"(.*?)\"[^>]*>", RegexOptions.Multiline);

html = oRegex1.Replace(html, delegate(Match m) {
    string title = m.Groups[1].Value;
    Regex oRegex2 = new Regex("<!-- #BeginEditable \"" + title + "\"[^>]*>(.*?)<!-- #EndEditable [^>]*>", RegexOptions.Multiline);
    return oRegex2.Match(regions).Groups[1].Value;
});