unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, RegularExpressions;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private function MyMatchEvaluator(const Match: TMatch): string; public end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);const pattern = '&#x?[0-9A-Fa-f]{1,5};';var List: TStringList; reg: TRegEx; path,tmpName: string;begin with TOpenDialog.Create(nil) do begin Execute; path := FileName; Free; end; if path = '' then Exit; List := TStringList.Create; List.LoadFromFile(path); List.Text := StringReplace(List.Text, 'GB2312', 'UTF-8', [rfIgnoreCase]); reg := TRegEx.Create(pattern, [roCompiled]); List.Text := reg.Replace(List.Text, MyMatchEvaluator); tmpName := ExtractFileName(path); path := StringReplace(path, tmpName, 'UTF8_' + tmpName, [rfIgnoreCase]); Text := path; List.SaveToFile(path, TEncoding.UTF8); List.Free;end;function TForm1.MyMatchEvaluator(const Match: TMatch): string;begin Result := Match.Groups[1].Value; if Match.Value[3] = 'x' then Result := '$' + Result; Result := WideChar(StrToInt(Result));end;end.
联系客服