string s = ".."; Regex re = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline); Match m = re.Match(s); if(m.Success) { string link = m.Groups["href"].Value; string text = Regex.Replace(m.Groups["text"].Value,"<[^>]*>",""); Console.WriteLine("link:{0}\ntext:{1}", link, text); }
string pattern = @"<a.*>(?<v>.*?)</a>"; Regex r; Match m; r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); for (m = r.Match(inputString); m.Success; m = m.NextMatch()) { Console.WriteLine("找到值 " + m.Groups["v"].Value); result.Add(m.Groups["v"].Value); } 请问在此基础上如何修改?这段代码目前只能取出a3
public static List<string> HrefTofields(this string inputString) { List<string> result = new List<string>(); string pattern = @"<a.*?>(?<v>.*?)</a>"; Regex r; Match m; r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); for (m = r.Match(inputString); m.Success; m = m.NextMatch()) { result.Add(m.Groups["v"].Value); } return result; }
public static List<string> HrefTofields(this string inputString) { List<string> result = new List<string>(); string pattern = @"<a.*?>(?<v>.*?)</a>"; Regex r; Match m; r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); for (m = r.Match(inputString); m.Success; m = m.NextMatch()) { result.Add(m.Groups["v"].Value); } return result; }
Regex re = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = re.Match(s);
if(m.Success)
{
string link = m.Groups["href"].Value;
string text = Regex.Replace(m.Groups["text"].Value,"<[^>]*>","");
Console.WriteLine("link:{0}\ntext:{1}", link, text);
}
string pattern = @"<a.*>(?<v>.*?)</a>";
Regex r;
Match m;
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
{
Console.WriteLine("找到值 " + m.Groups["v"].Value);
result.Add(m.Groups["v"].Value);
}
请问在此基础上如何修改?这段代码目前只能取出a3
{
List<string> result = new List<string>();
string pattern = @"<a.*?>(?<v>.*?)</a>";
Regex r;
Match m;
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
{
result.Add(m.Groups["v"].Value);
}
return result;
}
public static List<string> HrefTofields(this string inputString)
{
List<string> result = new List<string>();
string pattern = @"<a.*?>(?<v>.*?)</a>";
Regex r;
Match m;
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
{
result.Add(m.Groups["v"].Value);
}
return result;
}