想从<a onmouseup="1231" href="www.a.com" target="_self">张三</a>中取出www.a.com

解决方案 »

  1.   


                string a = @"<a onmouseup=""1231"" href=""www.a.com"" target=""_self"">张三</a>";
                string tempStr = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
                Regex re = new Regex(@"<a\s*onmouseup=""1231""\s*href=""(?<href>.*)""\s*target=""_self""\s*>(.*)</a>");
                string sr = re.Match(a).Groups["href"].Value;//www.a.com
      

  2.   

    这个onmouseup="1231" 不是唯一的,但是我只想匹配出<a>中含有onmouseup这个属性的herf值
    <a onmouseup="1" href="www.a.com" target="_self">张三</a>中取出www.a.com
    <a onmouseup="2" href="www.b.com" target="_self">李四</a>中取出www.b.com
      

  3.   

    把正则改成<a\s*onmouseup="(.*)"\s*href="(?<href>.*)"\s*target="_self"\s*>(.*)</a>            string a = @"<a onmouseup=""1231"" href=""www.a.com"" target=""_self"">张三</a>";
                Regex re = new Regex(@"<a\s*onmouseup="(.*)"\s*href="(?<href>.*)"\s*target="_self"\s*>(.*)</a>");
                string sr = re.Match(a).Groups["href"].Value;//www.a.com
      

  4.   

    改一下,上面那个有错            string a = @"<a onmouseup=""1231"" href=""www.a.com"" target=""_self"">张三</a>";       
                Regex re = new Regex(@"<a\s*onmouseup=""(.*)""\s*href=""(?<href>.*)""\s*target=""_self""\s*>(.*)</a>");         
                string sr = re.Match(a).Groups["href"].Value;
      

  5.   

    Regex re = new Regex(@"(?<=<a\s*onmouseup=""[^""]+""\s*href="")[^""]+(?="")", RegexOptions.None);
    MatchCollection mc = re.Matches("你要提取的字符串");
    foreach (Match ma in mc)
    {
      //ma.Value就是你要的值
    }
      

  6.   

    如果要匹配多条,用以下代码            Regex re = new Regex(@"<a\s*onmouseup=""([^""]*)""\s*href=""(?<href>.*)""\s*target=""[^""]""\s*>(.*)</a>");
                List<string> lists = new List<string>();
                MatchCollection sr = re.Matches(a);            foreach (Match me in sr)
                {
                    lists.Add(me.Value);//结果在lists
                }