把正则改成<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
改一下,上面那个有错 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;
Regex re = new Regex(@"(?<=<a\s*onmouseup=""[^""]+""\s*href="")[^""]+(?="")", RegexOptions.None); MatchCollection mc = re.Matches("你要提取的字符串"); foreach (Match ma in mc) { //ma.Value就是你要的值 }
如果要匹配多条,用以下代码 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 }
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
<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
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
Regex re = new Regex(@"<a\s*onmouseup=""(.*)""\s*href=""(?<href>.*)""\s*target=""_self""\s*>(.*)</a>");
string sr = re.Match(a).Groups["href"].Value;
MatchCollection mc = re.Matches("你要提取的字符串");
foreach (Match ma in mc)
{
//ma.Value就是你要的值
}
List<string> lists = new List<string>();
MatchCollection sr = re.Matches(a); foreach (Match me in sr)
{
lists.Add(me.Value);//结果在lists
}