日期:2014-05-18 浏览次数:20797 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace justTest { class RegularExp { public static void getSearchData() { [color=#FF0000]//string _regularExp = "<span class=[^>].*>(.*?)</"; string _regularExp = "<div class=\"zitifree\">\\s([^<].*)\\s</div>|<span class=[^>].*>(.*?)</";[/color] string _content0 = "<div class=\"zitifree\">\n历下区义忠汽车修理厂\n</div>\n\n</div>\n<div class=\"right\">\n</div>\n</div>"; string _content1 = "\n<div class=\"xian\">\n</div>\n<div class=\"content\">\n<div class=\"sidenei\">\n<div class=\"nei\">\n联 系 人: <span class=\"lblLinkMan\">王立强</span>"; string _content2 = "\n</div>\n<div class=\"nei\">\n联系电话: <span class=\"lbltel\">053186950690</span>"; string _content3 = "\n</div>\n<div class=\"nei\">\n邮政编码: <span class=\"lblPostalCode\">250013</span>\n</div>\n<div class=\"nei\">\n电子邮箱: <span class=\"lblemail\">aaa@gmail.com</span>\n</div>\n<div class=\"nei\">\n公司地址: <span class=\"lblweb\">山东省济南市历下区历山路36号</span>\n</div>\n</div>\n</div>"; string _contentAll = _content0 + _content1 + _content2 + _content3; Regex regexName = new Regex(_regularExp, RegexOptions.None); Match matchName = regexName.Match(_contentAll); int i = 0; bool regularState = true; searchData ues=new searchData(); while (matchName.Success) { string text1 = matchName.Result("$1").Trim(); //利用正则表达式来获取search对象的详细内容 switch (i) { case 0: ues.Name = text1; break; case 1: ues.Person = text1; break; case 2: ues.Photo = text1; break; case 3: ues.Youbian = text1; break; case 4: ues.Youxiang = text1; break; case 5: ues.Address = text1; break; //出现这种情况下说明出错了 匹配正则表达式 出现错误 default: regularState = false; break; } matchName = matchName.NextMatch(); i++; } Console.WriteLine(ues.Name); Console.WriteLine(ues.Person); Console.WriteLine(ues.Photo); Console.WriteLine(ues.Youbian); Console.WriteLine(ues.Youxiang); Console.WriteLine(ues.Address); } } public struct searchData { public string Name; public string Person; public string Photo; public string Youbian; public string Youxiang; public string Address; } }
=> string _regularExp = "<div class=\"zitifree\">\\s+(?<value>[^<].*)\\s+</div>|<span class=[^>].*>(?<value>.*?)</";