//读取网页html string text = File.ReadAllText(Environment.CurrentDirectory + "//test.txt", Encoding.GetEncoding("gb2312")); string prttern = "<a(\\s+(href=\"(?<url>([^\"])*)\"|‘([^‘])*‘|\\w+=\"(([^\"])*)\"|‘([^‘])*‘))+>(?<text>(.*?))</a>"; var maths = Regex.Matches(text, prttern); //抓取出来写入的文件 using (FileStream w = new FileStream(Environment.CurrentDirectory + "//wirter.txt", FileMode.Create)) { for (int i = 0; i < maths.Count; i++) { byte[] bs = Encoding.UTF8.GetBytes(string.Format("链接地址:{0}, innerhtml:{1}", maths[i].Groups["url"].Value, maths[i].Groups["text"].Value) + "\r\n"); w.Write(bs, 0, bs.Length); Console.WriteLine(); } } Console.ReadKey();
图解正则
时间: 2024-10-05 04:45:18