在Delphi Xe中使用带有rawbytestring的Regex

Dav*_*d A 2 regex delphi delphi-xe3

在我的项目中,我需要使用正则表达式在400mb TMemoryStream对象中查找一些数据.我在delphi xe3中检查新的regularrexpresion,但函数只与接收到的字符串参数匹配,而不是rawbytestring或pointer.我通过这种方式定义了模式:

MyPatt:="\x8A\x8A(..)\x8A"
Run Code Online (Sandbox Code Playgroud)

问题是如何找到我试过的二进制rawdata里面

TRegex.Match((MyStreamObject.Memory)^,MyPatt);
Run Code Online (Sandbox Code Playgroud)

但没有成功.我试着用这个而不是成功

TRegex.Match(String((MyStreamObject.Memory)^),MyPatt);
Run Code Online (Sandbox Code Playgroud)

bcz问题是如果rawbinary对象以0x00开头被截断.

我如何使用指针或rawbinarystring匹配正则表达式.

Arn*_*hez 6

您可以直接使用RegEx库API而不是基于字符串的Delphi类,这些类具有一些已识别(而非固定)的性能问题.

例如(与Delphi 6兼容至XE5):

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);
Run Code Online (Sandbox Code Playgroud)

此代码会比快得多TRegEx(并从其转换string为UTF-8)和TPerlRegEx作为限定RegularExpressionsCore.pas(其不设置PCRE_NO_UTF8_CHECK这样是很慢).

您可以在我们的REGEXP运算符中找到SQLite3单元的上述示例的原始代码.

  • 使RTL包装器变慢的原因不仅是UTF-8转换,而且更多的事实是PCRE执行选项是*硬编码*,并且未设置`PCRE_NO_UTF8_CHECK` - 请参阅我的链接和[Andreas提出的修复 - 其中我在答案中提到了这一点(https://forums.embarcadero.com/message.jspa?messageID=495865#495865).仍然[未修复](http://qc.embarcadero.com/wc/qcmain.aspx?d=108941).我怀疑EMB的表现并不重要 - 他们可能会写出与你相同的评论:表现根本不相关.在400 MB文件:相关速度快1000倍.:) (2认同)