Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-02

Replacing parts of a string with TRegexpr

2 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,
I'd like to replace some part of a string acording to a pattern. I know how to search and print the matches of a ER:

program test;
{$mode objfpc}{$H+}
uses
regexpr;
var
r: TRegexpr;
BEGIN
r := TRegexpr.create('(\d+)');
try
if r.exec('01 Brazil, 02 USA, 03 China') then
begin
repeat
writeln(r.match[0]);
until not r.ExecNext;
end
else
writeln('Pattern not found');
finally
r.free;
end;
END.

But how could I to substitute all "(\d+)", for example, to "xx". The regexpr unit has some documentation in its source code, but dispite this, I was not able to understand how to do it.

Luciano de Souza
luciano de souza
to luciano de souza

I am blind and it's not so easy to read the source code with so many comments. Dispite this, I find the method "TRegexpr.replace", accepting as the first argument the input string and the second the template string. The return is what I want.
In other words, the problem is solved and anything else is needed! Sorry for the unnecessary question!

2018-02-22 10:38 GMT-03:00, luciano de souza :

Show quoted text 36 lines
Hello all, I'd like to replace some part of a string acording to a pattern. I know how to search and print the matches of a ER: program test; {$mode objfpc}{$H+} uses regexpr; var r: TRegexpr; BEGIN r := TRegexpr.create('(\d+)'); try if r.exec('01 Brazil, 02 USA, 03 China') then begin repeat writeln(r.match[0]); until not r.ExecNext; end else writeln('Pattern not found'); finally r.free; end; END. But how could I to substitute all "(\d+)", for example, to "xx". The regexpr unit has some documentation in its source code, but dispite this, I was not able to understand how to do it. -- Luciano de Souza
Luciano de Souza