I am processing a number of strings from a TStringList and want to skip some lines that do not match a certain RegEx Pattern. Therefore I created a pattern of ^(?!\t\w+\t\w+) and attempted
program P;
uses
System.SysUtils, System.Classes, System.RegularExpressions;
var
S: TStringList;
I: Integer;
begin
S := TStringList.Create;
try
//Test and empty string should be passed
S.Add('Test'); S.Add(''); S.Add(#9'Hello'#9'world%%');
I := 0;
while ((I < S.Count - 1) and TRegex.IsMatch(S[I], '^(?!\t\w+\t\w+)', [])) do
Inc(I);
Writeln(IntToStr(I) + ': ' + S[I]);
Readln;
finally
S.Free;
end;
end.
Surprisingly it prints 1: thus matches the empty string from my StringList, though it should match the pattern. I can catch this case by adding and S[I] <> '' but I'm wondering if I missed any Regex option (or similar) to correctly handle empty strings with a RegEx. I had to explicitly use empty RegexOptions in the IsMatch function, as roNotEmpty is used per default - but this only allows my pattern to match for a zero-length.
I have tested this in Delphi 10.1.
This is a known issue.
You can recompile the unit after modifying the code as mentioned in the comments of the issue. All you have to do is to explicitly add the pas file to your project to cause the compiler to recompile it instead of using the shipped dcu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With