I have a problem using TRegEx.replace
:
var
Value, Pattern, Replace: string;
begin
Value := 'my_replace_string(4)=my_replace_string(5)';
Pattern := 'my_replace_string\((\d+)\)';
Replace := 'new_value(\1)';
Value := TRegEx.Replace(Value, Pattern, Replace);
ShowMessage(Value);
end;
The expected result would be new_value(4)=new_value(5)
, while my code (compiled with Delphi XE4) gives new_value(4)=new_value()1)
With Notepad++, I get the expected result.
Using a named group makes it clear the 1
is the backreference treated literally:
Pattern := 'my_replace_string\((?<name>\d+)\)';
Replace := 'new_value(${name})';
// Result: 'new_value(4)=new_value(){name})'
The replacement is always that simple (could be zero or more times my_replace_string
), so I could easily create a custom search-and-replace function, but I'd like to know what goes on here.
Is it my fault or is it a bug?