Back

PRF018

Regex in loop

Object Pascal — Delphi & Free Pascal
Warning
Performance

Creating or executing TRegEx inside a loop recompiles the pattern on each iteration

creating or executing TRegEx inside a loop recompiles the pattern on each iteration.

Why this severity

creating or executing TRegEx inside a loop recompiles the pattern on each iteration.


Known false positives

loops with very few iterations where compilation cost is negligible.

Remediation example

RE := TRegEx.Create(Pattern);
for I := 0 to Count - 1 do
  if RE.IsMatch(Items[I]) then
    Process(Items[I]);

Reconnecting… Connection lost. Reload