日期:2014-05-17  浏览次数:20484 次

已经写好的正则如果增加一点功能,请指点
html字符:
<ST:Calendar style="width:155px;height:22px;" id="C_1_1_2_cal_t" src="Control/Calendar.gif" tooltip="工程申报时间" runat="server" format="money" fontstyle="" isrequired="true" /></ST:Calendar>
....
<ST:TextBox style="width:160px;height:25px;" id="C_1_1_2_t" src="Control/TextBox.gif" tooltip="批准文号" runat="server" format="string" fontstyle="" isrequired="true" /></ST:TextBox>

正则表达式:
        var list = Regex.Matches(htmlstr, @"(?is)<ST:(?=[^>]*?id=([""'])(?<id>[^""']+)\1)(?=[^>]*?tooltip=([""'])(?<tooltip>[^""']+)\2)(?=[^>]*?style=([""'])(?<style>[^""']+)\3){0,1}(?=[^>]*?format=([""'])(?<format>[^""']+)\4){0,1}[^>]*>").OfType<Match>().Select(t => new { id = t.Groups["id"].Value, tooltip = t.Groups["tooltip"].Value, style = t.Groups["style"].Value, format = t.Groups["format"].Value }).ToList();
    if (list.Count > 0)
        {
            foreach (var obj in list)
            {
              Response.Write("id:" + obj.id + " tooltip:" + obj.tooltip + " style:" + obj.style + " <br />");
              //增加一个obj.Name (怎么获取这个ST:后面的值)
            }
        }

目前 能 取出 正则里的值,我现在需要加一个值
就是能获取到ST:后面的值 该怎么增加
如上面的html字符里的 <ST:Calendar 要获取到 Calendar <ST:TextBox 要获取到 TextBox

------解决方案--------------------

string htmlstr = @"<ST:Calendar style=""width:155px;height:22px;"" id=""C_1_1_2_cal_t"" src=""Control/Calendar.gif"" tooltip=""工程申报时间"" runat=""server"" format=""money"" fontstyle="""" isrequired=""true"" /></ST:Calendar>
<ST:TextBox style=""width:160px;height:25px;"" id=""C_1_1_2_t"" src=""Control/TextBox.gif"" tooltip=""批准文号"" runat=""server"" format=""string"" fontstyle="""" isrequired=""true"" /></ST:TextBox>";
            string pattern = @"(?is)<ST:(?<ST>\w+)\s*\b[^>]*?id=([""'])(?<id>[^""']+)\1[^>]*?tooltip=([""'])(?<tooltip>[^""']+)\2(?=[^>]*?style=([""'])(?<style>[^""']+)\3){0,1}(?=[^>]*?format=([""'])(?<format>[^""']+)\4){0,1}[^>]*>";
            var list = Regex.Matches(html