110,537
社区成员
发帖
与我相关
我的任务
分享
string s = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6"">
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">";
MatchCollection matches = Regex.Matches(s, @"(?is)<a href=""(.*?)"".+?title=""(.*?)"">");
foreach (Match match in matches)
Console.WriteLine(match.Groups[1].Value + "\t" + match.Groups[2].Value);
string input = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6"">
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">
";
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(input, @"(?is)<a\b[^>]*?href=([""'\s]?)/xxx/(\d+)\.shtml\1[^>]*?title=([""'\s]?)([^""']*?)\3[^>]*?>"))
{
dic.Add(m.Groups[2].Value, m.Groups[4].Value);
}
foreach (var m in dic)
{
Console.WriteLine(m.Key + "\t" + m.Value);
}
StreamReader reader = new StreamReader("c:\\1.txt");
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)(?<num>\d+).shtml[^>]*title=""(?<title>[^>]*)""");
MatchCollection mc = reg.Matches(source);
string[] ss = new string[mc.Count];
for (int i = 0; i < ss.Length; i++)
{
ss[i] = mc[i].Groups["num"].Value + ":" + mc[i].Groups["title"].Value;
}