Delphi 10.1 - Invalid Match. Know in TMatchEvaluator when TRegEx.Replace () is called
I found one bug with the latest Delphi 10.1 Berlin (and 10.2 Tokyo too).
If you call TRegEx.Replace with the specified TMatchEvaluator, you might get the wrong TMatch function inside the Evaluator function. This works well in Delphi XE-XE5.
appraiser:
function TForm1.EvaluatorU(const Match: TMatch): string;
var
lChar: Word;
lMatchVal: string;
begin
Result := '';
lMatchVal := Match.Groups[1].Value;
lChar := StrToIntDef('$'+lMatchVal, 0);
if lChar <> 0 then
Result := Char(lChar);
end;
Call:
Result := TRegEx.Replace('\u0418\u0443, \u0427\u0436\u044d\u0446\u0437\u044f\u043d', '\\u([0-9a-f]{4})', EvaluatorU, [roIgnoreCase]);
The first call to Evaluator will result in the context TMatch.Value (or TMatch.Group []. Value), but the second call will result in an incorrect Match.Value function for the callback (
Do you have any idea of a workaround?
I am going to test this issue on TPerlRegEx class, maybe something is wrong in wrapper functions (TRegEx).
Update: with replacing TPerlRegEx with a callback function (OnReplace) works well ...
Update2: There seems to be a bug in TPerlRegEx. It returns incorrect GroupOffsets options. The first time the OnReplace callback is called, this value is correct. The following calls return +1 offset more than necessary. But calling TPerlRegEx.Groups returns the correct subgroup value ...
Latest update and solution found :
I found the problem in optimizing TPerlRegEx.UTF8IndexToUnicode. There are LastIndex * / LastIndexResult * fields used to optimize sequential function calls with the same parameters. But after replacement done via callback functions and when MatchAgain is called in TPerlRegEx.ReplaceAll function it might do a bad trick (
A simple solution is to copy System.RegularExpressionsCore.pas from \ source \ rtl \ common to your project directory and replace the TPerlRegEx.UTF8IndexToUnicode call with the deprecated unoptimized UTF8IndexToUnicode function ... Or clear those internal fields somewhere in the ClearStoredGroups function, for example.
source to share
No one has answered this question yet
Check out similar questions: