Recent

Author Topic: use of replace function with Sorokin's regex library  (Read 2759 times)

Sibylla

  • New Member
  • *
  • Posts: 13
use of replace function with Sorokin's regex library
« on: December 19, 2014, 04:15:30 pm »
Hi,
I wonder how to use the replace function of Sorokin's regex library. I guess we need a variable such as:

re: TRegExpr;
MyReplaceFunction: TRegExprReplaceFunction;

But I didn't succeed in implementing the replace call with a replace function: re.Replace(... .

A

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: use of replace function with Sorokin's regex library
« Reply #1 on: December 19, 2014, 06:43:53 pm »
A replace function has to be a member function of some class (a form, for instance):
Code: [Select]
  TForm1 = class(TForm)
...
    function MyReplaceFunc(ARegExpr : TRegExpr): string;
...
implementation
...
function TForm1.MyReplaceFunc(ARegExpr: TRegExpr): string;
begin
  Result := 'Whatever your function needs to return';
end;

When you call re.Replace, pass @MyReplaceFunc or MyReplaceFunc based on the compiler mode you used.
Code: [Select]
...
{$mode objfpc}  //<----
...
  AfterReplacement := re.Replace(Expr, @MyReplaceFunc);

Code: [Select]
...
{$mode delphi} //<----
...
  AfterReplacement := re.Replace(Expr, MyReplaceFunc);

Sibylla

  • New Member
  • *
  • Posts: 13
Re: use of replace function with Sorokin's regex library
« Reply #2 on: December 21, 2014, 08:08:06 pm »
Thanks so much. It works nicely!
Here is the code snippet for those interested:

  re.Expression:= LabeledEdit1.Text; // the regular expression.
  input := Memo1.Text; // the text.
  s := re.Replace(input, @MyReplaceFunc);   

 

TinyPortal © 2005-2018