Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must free TRegEx object after TRegEx.Create?

I have seen several Delphi examples of TRegEx usage like the following one in Delphi 10.1.2:

try
  RegexObj := TRegEx.Create(REGEX_EXTRACTEMAILADDRESSES, [roIgnoreCase]); 
  MatchResults := RegexObj.Match(ThisPageText);
  while MatchResults.Success do
  begin
    slEmailAddressesOnThisPage.Add(MatchResults.Value);
    MatchResults := MatchResults.NextMatch();
  end;
except
  on E: ERegularExpressionError do
  begin
    // Todo: Log Syntax error in the regular expression
  end;
end;

So I wonder whether the TRegEx object must be explicitly freed after creation in such an example?

like image 452
user1580348 Avatar asked Aug 11 '17 21:08

user1580348


People also ask

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

Why do we need regular expression?

Regular expressions are particularly useful for defining filters. Regular expressions contain a series of characters that define a pattern of text to be matched—to make a filter more specialized, or general. For example, the regular expression ^AL[.]* searches for all items beginning with AL.

What is the use of given statement in regular expression a za Z?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.


1 Answers

Only class objects that derive from TObject must be explicitly freed from memory after being created. TRegEx is a record instead, so it is released when it goes out of scope. TRegEx.Create is a constructor, but not one that creates a new object on the heap, just on the call stack, so there is nothing to free manually (there is no destructor defined for it).

like image 104
Remy Lebeau Avatar answered Sep 18 '22 16:09

Remy Lebeau