字符串提取算法


本文整理自网络,侵删。

 private

FPageString:string;
FCurDir :string;
FHost :string;


function Tform1.GetLinks(Links: TStrings): integer;
var iLen, i: integer;
sLink: string;
boHref, boImg, boStart, boOther :Boolean;
ch :char;
p :PChar;
s :array[0..255] of char;
label addchEnd;
begin

result := -1;
Links.Clear;

iLen := length (FPageString);

boHref := False;
boImg := False;
boStart := False;
boOther := False;

sLink := '';

p := PChar (FPageString);

i := -1;

While True do begin
Inc (i);
if i >= iLen - 1 then break;
ch := p[i];
if boOther then begin
if ch = '>' then begin
boOther := False;
end ;
Continue;
end;

if (not boHref) and (not boImg) then begin
if i + 10 >= iLen then break;
if p[i] <> '<' then
continue;

if p[i+1] in ['A', 'a'] then begin
if p [i+2] = ' ' then begin
boHref := True;
boStart := False;
sLink := '';
Inc (i, 2);
Continue;
end;
end else

if p[i+1] in ['I', 'i'] then begin
StrLCopy ( @s, @p[i], 5);
s[5] := #0;

if SameText (s, '<img ') then begin
boImg := True;
boStart := False;
sLink := '';
Inc (i, 4);
Continue;
end;
end;

boOther := True;
continue;
end;

if ch = '>' then begin
goto addchEnd;
end;

if not BoStart then begin
if boHref then begin
StrLCopy ( @s, @p[i], 5);
s[5] := #0;
if SameText (s, 'href=') then begin
boStart := True;
Inc (i, 4);

end;
end;

if boImg then begin
StrLCopy ( @s, @p[i], 4);
s[4] := #0;
if SameText (s, 'src=') then begin
boStart := True;
Inc (i, 3);
end;
end;

continue;
end;

if ch in [' ', '"', #39] then begin
if sLink <> '' then begin

goto addchEnd;
end;
Continue;
end;

if (ch = '.') and (p[i+1] = '/') then begin
Inc (i);
continue;
end;

sLink := sLink + ch;
continue;

addchEnd:
boHref := False;
boImg := False;
boStart := False;

if sLink <> '' then begin
if not SameText ('http://', Copy (sLink, 1, 7)) then
begin
if sLink[1] <> '/' then
sLink := FCurDir + sLink
else
sLink := FHost + sLink;
end;
Links.Add(sLink)
end;

end;

result := Links.Count;
end;


相关阅读 >>

Delphi气泡提示

Delphi 的tjpegimage跟image区别

Delphi 的编译指令(1): $define、$undef、$ifdef、$else、$endif

Delphi 数组类型与数组指针的巧妙利用

Delphi编程用流来读取txt文件中的数据

Delphi listview 导出excel txt vcf 单元

Delphi 文字的高度与宽度: canvas.textextent

Delphi判断字符串是否是汉字

Delphi 上传文件到七牛,纯原生

Delphi 中showmodal与show的区别

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...