gpt4 book ai didi

regex - 在带有 rawbytestring 的 Delphi Xe 中使用正则表达式

转载 作者:行者123 更新时间:2023-12-03 18:18:57 25 4
gpt4 key购买 nike

在我的项目中,我需要使用正则表达式在 400mb TMemoryStream 对象中查找一些数据。我正在检查 delphi xe3 中的新正则表达式,但函数只匹配接收到的字符串参数,而不匹配原始字节串或指针。我以这种方式定义了模式 ok:

MyPatt:="\x8A\x8A(..)\x8A"

问题是二进制rawdata里面怎么找我尝试了

TRegex.Match((MyStreamObject.Memory)^,MyPatt);

但没有成功。我尝试了这个,但也不成功

TRegex.Match(String((MyStreamObject.Memory)^),MyPatt);

问题是如果以 0x00 开头的原始二进制对象被截断了。

我如何使用指针或原始二进制字符串匹配正则表达式。?

最佳答案

您可以直接使用 RegEx 库 API 而不是基于字符串的 Delphi 类,后者有一些 identified (and not fixed) performance issues .

例如(兼容 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);

这段代码将比TRegEx(以及它从string到UTF-8的转换)和中定义的TPerlRegEx快得多>RegularExpressionsCore.pas(未设置 PCRE_NO_UTF8_CHECK,因此非常慢)。

你可以找到上面示例的原始代码in our REGEXP operator for SQLite3 unit .

关于regex - 在带有 rawbytestring 的 Delphi Xe 中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19286404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com