delphi使用正则匹配网页数据
代码中用到了IdAntiFreeze、idhttp、PerlRegEx和StringGrid。
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; 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号