C# 在替换部分正则表达式匹配项之前,请先对其进行处理

C# 在替换部分正则表达式匹配项之前,请先对其进行处理,c#,asp.net,regex,C#,Asp.net,Regex,我正在编写一个函数,它将解析一个类似于遗留系统中的XML文件的文件 .... <prod pid="5" cat='gov'>bla bla</prod> ..... <prod cat='chi'>etc etc</prod> .... ..... 最终输出应为: <span class='prod'>bla bla</span><span class='cat'>Government</span>

我正在编写一个函数,它将解析一个类似于遗留系统中的XML文件的文件

....
<prod pid="5" cat='gov'>bla bla</prod>
.....
<prod cat='chi'>etc etc</prod>
....
.....
最终输出应为:

<span class='prod'>bla bla</span><span class='cat'>Government</span>
blabla政府
你知道我该怎么做吗

注1:该功能已完成(本部分除外),工作正常。
注2:无法使用XML库,必须使用正则表达式。Replace有一个重载,它使用了一个匹配计算器,它基本上是一个函数。因此,您可以动态生成替换字符串

buf = Regex.Replace(entry, @"<prod(?<attr>.*?)>(?<text>.*?)</prod>", match => {
    var attrText = match.Groups["attr"].Value;
    var text = match.Groups["text"].Value;

    // Now, parse your attributes
    var attributes = Regex.Matches(@"(?<name>\w+)\s*=\s*(['""])(?<value>.*?)\1")
                          .Cast<Match>()
                          .ToDictionary(
                               m => m.Groups["name"].Value,
                               m => m.Groups["value"].Value);

    string category;
    if (attributes.TryGetValue("cat", out category))
    {
        // Your SQL here etc...
        var label = GetLabelForCategory(category)
        return String.Format("<span class='prod'>{0}</span><span class='cat'>{1}</span>", WebUtility.HtmlEncode(text), WebUtility.HtmlEncode(label));
    }

    // Generate the result string
    return String.Format("<span class='prod'>{0}</span>", WebUtility.HtmlEncode(text));
});
buf=Regex.Replace(条目,@“(?*)”,匹配=>{
var attrText=match.Groups[“attr”].Value;
var text=match.Groups[“text”].Value;
//现在,解析您的属性
var attributes=Regex.Matches(@“(?\w+)\s*=\s*(['”“])(?*??\1”)
.Cast()
.ToDictionary(
m=>m.Groups[“name”].Value,
m=>m.Groups[“value”].value);
字符串类别;
if(attributes.TryGetValue(“cat”,超出类别))
{
//你的SQL在这里等。。。
var label=GetLabelForCategory(类别)
返回String.Format(“{0}{1}”、WebUtility.HtmlEncode(text)、WebUtility.HtmlEncode(label));
}
//生成结果字符串
返回String.Format(“{0}”,WebUtility.HtmlEncode(text));
});

这应该可以让您开始了。

为什么不能使用XML库呢?非常感谢。效果很好。我不得不将match=>{”替换为“delegate(match-match){”,以使其工作。不客气。顺便说一句,如果您想保留lambda表示法,此语法也应该工作:
(match-match)=>{…}
<span class='prod'>bla bla</span><span class='cat'>Government</span>
buf = Regex.Replace(entry, @"<prod(?<attr>.*?)>(?<text>.*?)</prod>", match => {
    var attrText = match.Groups["attr"].Value;
    var text = match.Groups["text"].Value;

    // Now, parse your attributes
    var attributes = Regex.Matches(@"(?<name>\w+)\s*=\s*(['""])(?<value>.*?)\1")
                          .Cast<Match>()
                          .ToDictionary(
                               m => m.Groups["name"].Value,
                               m => m.Groups["value"].Value);

    string category;
    if (attributes.TryGetValue("cat", out category))
    {
        // Your SQL here etc...
        var label = GetLabelForCategory(category)
        return String.Format("<span class='prod'>{0}</span><span class='cat'>{1}</span>", WebUtility.HtmlEncode(text), WebUtility.HtmlEncode(label));
    }

    // Generate the result string
    return String.Format("<span class='prod'>{0}</span>", WebUtility.HtmlEncode(text));
});