我在 Delphi Rio 10.3.3 中使用 System.RegularExpressions 编写了此代码:
var
S: string;
Itf: string; // Interface section
TempList: TStringList;
ThisRegexObj: System.RegularExpressions.TRegEx;
ThisMatchResult: System.RegularExpressions.TMatch;
begin
ThisRegexObj := System.RegularExpressions.TRegEx.Create(
'((?<=procedure ).*(?=\())|(?<=procedure ).*(?!\()', [roIgnoreCase]);
ThisMatchResult := ThisRegexObj.Match(Itf);
TempList := TStringList.Create;
try
while ThisMatchResult.Success do
begin
TempList.Add(ThisMatchResult.Value);
ThisMatchResult := ThisMatchResult.NextMatch;
end;
mmoTokens.Lines.Add('Interface Procedure names: ' + TempList.CommaText);
finally
TempList.Free;
end;
之后必须释放对象ThisRegexObj和ThisMatchResult的内存吗?
最佳答案
Must memory of created System.RegularExpressions objects be released?
对象是类的实例,并且它们总是需要被释放。
但是,TRegEx
和 TMatch
根本不是类,而是记录类型。因此,您的 ThisRegexObj
和 ThisMatchResult
不是对象,而是记录。并且记录由编译器自动处理。
因此,您不需要释放任何内容(实际上,您不能这样做 - 这些记录中没有免费
成员)。
关于regex - 必须释放创建的 System.RegularExpressions 对象的内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59234114/