已经保存到本地硬盘的页面如:test.html
内容:
《笔,种在脚下才开花》 1,2,3
《冰雪无情人有情》 4,5,6
。
现在想把上面的纯数字转换成带链接的字符串
如
《笔,种在脚下才开花》 <A href=page1.html> 1 </A> <A href=page2.html> 2 </A> <A href=pige3.html> 3 </A>
《冰雪无情人有情》 <A href=page4.html> 4 </A> <A href=page5.html> 5 </A> <A href=pige6.html> 6 </A>
。
但由于受到html代码参数
<TD COLSPAN=1 HEIGHT=25>
<TR VALIGN="TOP" ALIGN="LEFT">
<TD COLSPAN=1 HEIGHT=14>
<TR VALIGN="TOP" ALIGN="LEFT">
<TD COLSPAN=5>
<TD COLSPAN=2 HEIGHT=30 WIDTH=97
VALIGN="TOP">
的影响,不能直接用stringreplace函数处理
请问那位高手帮帮忙可以解决这个问题!
内容:
《笔,种在脚下才开花》 1,2,3
《冰雪无情人有情》 4,5,6
。
现在想把上面的纯数字转换成带链接的字符串
如
《笔,种在脚下才开花》 <A href=page1.html> 1 </A> <A href=page2.html> 2 </A> <A href=pige3.html> 3 </A>
《冰雪无情人有情》 <A href=page4.html> 4 </A> <A href=page5.html> 5 </A> <A href=pige6.html> 6 </A>
。
但由于受到html代码参数
<TD COLSPAN=1 HEIGHT=25>
<TR VALIGN="TOP" ALIGN="LEFT">
<TD COLSPAN=1 HEIGHT=14>
<TR VALIGN="TOP" ALIGN="LEFT">
<TD COLSPAN=5>
<TD COLSPAN=2 HEIGHT=30 WIDTH=97
VALIGN="TOP">
的影响,不能直接用stringreplace函数处理
请问那位高手帮帮忙可以解决这个问题!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus;type
TForm1 = class(TForm)
btnReplace: TButton;
procedure btnReplaceClick(Sender: TObject);
private
procedure DoReplace(num: Integer; var S: string);
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DoReplace(num: Integer; var S: string);
const
REP_FMT = '<A href=page%d.html> %d </A>';
begin
S := Format(REP_FMT, [num, num]);
end;procedure TForm1.btnReplaceClick(Sender: TObject); function ParseString(const S: string): string;
var
i: Integer;
inNum, isCurNum: Boolean;
NumStr, Replacer: string;
begin
inNum := False;
NumStr := '';
Result := '';
for i := 1 to Length(S) do
begin
isCurNum := S[i] in ['0'..'9'];
if isCurNum then
NumStr := NumStr + S[i]; if inNum and (not isCurNum or (i = Length(s))) then
begin
Replacer := NumStr;
DoReplace(StrToInt(NumStr), Replacer);
Result := Result + Replacer;
end; if not isCurNum then
Result := Result + S[i]; inNum := isCurNum;
if not inNum then
NumStr := '';
end;
end;var
List: TStringList;
i: Integer;
S: string;
begin
List := TStringList.Create;
try
List.LoadFromFile('D:\1.txt');
for i := 0 to List.Count - 1 do
begin
S := ParseString(List[i]);
List[i] := S;
end;
List.SaveToFile('d:\2.txt');
finally
List.Free;
end;
end;end.