if you don't need to match those attributes, you can do<table[^>]*>

解决方案 »

  1.   

    to saucer(思归, MS .NET MVP):
    我是想
    除了我上面写的属性之外,如果再有别的东西,比如onclick我是不会匹配的,
    怎么办,你的方法连onclik都匹配了
      

  2.   

    帮帮忙吧,本人的另外一个贴的分也可以给他:
    http://expert.csdn.net/Expert/topic/2097/2097696.xml?temp=.5310938同一个问题
      

  3.   

    if you want to match everything, try
    string[] slist = {"<table width=\"100%\" height=\"\" border=\"0\" style=\"\" cellpadding=\"0\" cellspacing=\"0\">",
    "<table>","<table border=\"1\"  >", "<table border=\"1\" width=\"100%\"  test=\"ss\" >",
    "<table test=\"ss\" border=\"1\" width=\"100%\"   >"
    }
    ;
    Regex re = new Regex(@"<table(?:[^>]*?(?:width=""(?<width>[^""]*)""|height=""(?<height>[^""]*)""|border=""(?<border>[^""]*)""|style=""(?<style>[^""]*)""|cellpadding=""(?<cellpadding>[^""]*)""|cellspacing=""(?<cellspacing>[^""]*)""))*[^>]*?>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    foreach (string s in slist)
    {
     Console.WriteLine("\nfor:\t{0}\n",s);
    Match m  = re.Match(s);
    if (m.Success)
    {
     Console.WriteLine("width:{0}",m.Groups["width"].Value);
     Console.WriteLine("height:{0}",m.Groups["height"].Value);
     Console.WriteLine("border:{0}",m.Groups["border"].Value);
     Console.WriteLine("style:{0}",m.Groups["style"].Value);
     Console.WriteLine("cellpadding:{0}",m.Groups["cellpadding"].Value);
     Console.WriteLine("cellspacing:{0}",m.Groups["cellspacing"].Value);}
    }
    if you just want what you listed it, try
    string[] slist = {"<table width=\"100%\" height=\"\" border=\"0\" style=\"\" cellpadding=\"0\" cellspacing=\"0\">",
    "<table>","<table border=\"1\"  >", "<table border=\"1\" width=\"100%\"  test=\"ss\" >",
    "<table test=\"ss\" border=\"1\" width=\"100%\"   >"
    }
    ;
    Regex re = new Regex(@"<table(?:\s*(?:width=""(?<width>[^""]*)""|height=""(?<height>[^""]*)""|border=""(?<border>[^""]*)""|style=""(?<style>[^""]*)""|cellpadding=""(?<cellpadding>[^""]*)""|cellspacing=""(?<cellspacing>[^""]*)""))*\s*>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    foreach (string s in slist)
    {
     Console.WriteLine("\nfor:\t{0}\n",s);
    Match m  = re.Match(s);
    if (m.Success)
    {
     Console.WriteLine("width:{0}",m.Groups["width"].Value);
     Console.WriteLine("height:{0}",m.Groups["height"].Value);
     Console.WriteLine("border:{0}",m.Groups["border"].Value);
     Console.WriteLine("style:{0}",m.Groups["style"].Value);
     Console.WriteLine("cellpadding:{0}",m.Groups["cellpadding"].Value);
     Console.WriteLine("cellspacing:{0}",m.Groups["cellspacing"].Value);}
    }