已经保存到本地硬盘的页面如: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.   


    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.
      

  2.   

    太感谢blazingfire了,提供了很好的参考,但可以把html表格参数值也替换了!这个是问题关键!
      

  3.   

    <TD COLSPAN=1 HEIGHT=25> 25 </td>内的参数25也替换了,不是想要的效果!<td>25</td>内的替换才是要的
      

  4.   

    正则表达式的vcl?没有接触过哦!可以讲得详细点或者给点链接吗?
      

  5.   

    问题基本解决,强烈感谢“blazingfire ”的提示!马上给分!