字符串提取算法


本文整理自网络,侵删。

 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 opentextfiledialog用法

Delphi 简单的 "双缓冲" 绘图的例子

Delphi xe android platform uses-permission[2] androidmanifest.xml 配置

Delphi下pos机控制钱箱,客显,打印机

Delphi 从 twebbrowser中获得当前输入处的链接

Delphi mscomm1自动获取串口

Delphi中如何给一个字符串从左边进行补0

Delphi idhttp组件+idhttpserver组件实现文件下载服务

Delphi 关于 beep、messagebeep 和 windows.beep

Delphi编程如何判断图片文件的真实类型?

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



打赏

取消

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

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

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

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

评论

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