文字列ベースの Delphi クラスの代わりに、RegEx ライブラリ API を直接使用できます。Delphi クラスには、特定された(および修正されていない)パフォーマンスの問題がいくつかあります。
たとえば (XE5 までの Delphi 6 と互換性があります):
uses
{$ifdef ISDELPHIXE}
// use direct PCRE library as available since Delphi XE
RegularExpressionsAPI,
{$else}
// download from http://www.regular-expressions.info/download/TPerlRegEx.zip
PCRE,
{$endif}
SysUtils,
...
var
compiled: PPCRE;
extra: PPCREExtra;
errMsg: PAnsiChar;
errPos: integer;
// here regexp points to your null-terminated regular expression
compiled := pcre_compile(PAnsiChar(regexp),0,@errMsg,@errPos,nil);
if reg=nil then begin
CompileError;
exit;
end;
extra := pcre_study(compiled,0,@errMsg);
// now use the compiled pcre expression (once compiled, it is better to re-use compiled/extra values)
found := pcre_exec(compiled,extra,pointer(text),StrLen(text),0,PCRE_NO_UTF8_CHECK,nil,0)>=0;
// do not forget to release the compiled pcre expression
pcre_dispose(compiled,extra,nil);
このコードは (およびUTF-8 へのTRegEx
変換)よりもはるかに高速であり、で定義されているとおりです(これは設定されていないため、非常に低速です)。string
TPerlRegEx
RegularExpressionsCore.pas
PCRE_NO_UTF8_CHECK
上記のサンプルの元のコードは、SQLite3 unitの REGEXP 演算子にあります。