花褪残红青杏小。燕子飞时,绿水人家绕。

delphi使用正则匹配网页数据

菜鸟编程 十五楼的鸟儿 25236浏览 0评论
代码中用到了IdAntiFreeze、idhttp、PerlRegEx和StringGrid。
[code=delphi]
type
IdHTTP1: TIdHTTP;
IdAntiFreeze1: TIdAntiFreeze;
PerlRegEx1: TPerlRegEx;
StringGrid1: TStringGrid;
Edit1.Text:=s2;//可以设置成某内置变量,也可以手动输入
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
try //try来捕获错误,使用方便,哈哈。
s := idhttp1.get(edit1.text);
except
showmessage('未获取到数据'); //基本上是网页404了
end; //s是获取到的网页源码数据
reg := TPerlRegEx.Create(nil);
//下面是ID部分
reg.Subject := s;
reg.RegEx := '(?:EAV-\d+)';//本次匹配的是NOD32的账号 格式为 “EAV-”加上8个数字
sa := 1;
while reg.MatchAgain do
case sa of
16 : Break;//只匹配最前面的15个账号,所以在16的时候break
else ;
begin
stringgrid1.cells[0,sa] := inttostr(sa);//inttostr(sa)是获取sa的值
zhanghao:=StringReplace(reg.MatchedExpression,' ','',[rfReplaceAll]);//再简单的过滤下空格
stringgrid1.cells[1,sa]:=zhanghao;//填充到stringgrid中了。
StringGrid1.RowCount := StringGrid1.RowCount + 1;
sa :=sa+1;
end;
FreeAndNil(reg);//释放
end;
[/code]

转载请注明:鸟儿博客 » delphi使用正则匹配网页数据

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址