Replacing parts of a string with TRegexpr

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Replacing parts of a string with TRegexpr

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Replacing parts of a string with TRegexpr

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 <[hidden email]>:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal