关于winform正则表达式使用

Coder_delphi 2017-09-14 04:57:26
1. 最近想要写一个爬虫程序,爬的是起点 a.qidian.com。 抓去到网页的内容,用正则表达式分析时
public string RegxName = "<a.+?data-eid=\"qd_B58\".*>(.+)</a>";
返回了一个错误的集合,并没有把 data-eid="qd_B58" 的超链接筛选出来,但是使用网页上的,在线正则表达式时,可以正确的提取。
以下是,C# 解析的代码
public string RegxName = "<a.+?data-eid=\"qd_B58\".*>(.+)</a>";
public string RegxID = "\"(.*?)\"";
public void GetRegx(string sContent) {
string urlcontent = sContent;
try {
MatchCollection mc = Regex.Matches(urlcontent, RegxName,RegexOptions.Singleline);

foreach (Match match in mc) {
string GetStr = match.Value.ToString();

Regex regB = new Regex(RegxID);

MatchCollection mc1 = regB.Matches(GetStr);

foreach (Match mac in mc1) {

string idstr = mac.Value.ToString();
string iidstr = idstr;
}
}




}
catch(Exception ex) {
}
}
...全文
266 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MYsce 2017-09-15
  • 打赏
  • 举报
回复
引用 2 楼 q107770540 的回复:
public string RegxName = "<a\b[^>]*?data-eid=\"qd_B58\"[^>]*?>(.+?)</a>";
+1
q107770540 2017-09-15
  • 打赏
  • 举报
回复
public string RegxName = "<a\b[^>]*?data-eid=\"qd_B58\"[^>]*?>(.+?)</a>";
  • 打赏
  • 举报
回复

            string RegxName = "<a href=\"//([^\"]*)\"[^>]*?data-eid=\"qd_B58\" data-bid=\"([^\"]*?)\">(.+?)</a>";
            MatchCollection mc = Regex.Matches(content, RegxName);

            foreach (Match match in mc)
            {
                string bookAddress = match.Groups[1].Value;
                string bookID = match.Groups[2].Value;
                string bookName = match.Groups[3].Value;

                Console.WriteLine(bookAddress);
                Console.WriteLine(bookID);
                Console.WriteLine(bookName);
            }

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧