繁体   English   中英

在C#ASP.NET中使用RegEx Replace函数

[英]Using RegEx Replace function in C# ASP.NET

我该如何解决以下问题?

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

<html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html>

与此分开的是,数据库字段中的内容看起来像这样:

<!-- #BeginEditable "Body1" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable "Extra" -->This is more test text<!-- #EndEditable -->

如您所料,我需要将两者合并,即替换

<!-- #Editable "Body1" -->

与:

This is Test Text

我已经在这里开始了代码。 但是我在使用正则表达式替换功能时遇到了问题,该功能应该位于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
        //
        //
        //
    }

没有针对性能(或其他任何方面)进行优化,但是它很简单并且可以正常工作:

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);
}

最好使用HTML Agility Pack为您处理此问题,然后再使用正则表达式。 它可以将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));
}

请注意,我必须在适当的位置使用美元符号来防止转义,并在运行时将其替换为双引号。

希望这对您有所帮助,汤姆,谢谢。

我建议对这种东西使用像NVelocity这样的模板引擎。

使用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;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM