[fpc-pascal] RegExpr - replace

Marcos Douglas md at delfire.net
Sun Sep 7 02:54:35 CEST 2014


Well, I got it.
Is possible using  a combination of these : r.MatchPos and  r.MatchLen
for I := 1 to r.SubExprMatchCount do
begin
  S := Format('line %d pos:%d len:%d',[I, r.MatchPos[I], r.MatchLen[I]]);
  Memo1.Append(S);
end;

I can use StringReplace function to replace these values.

--
Marcos Douglas


On Sat, Sep 6, 2014 at 9:37 PM, Marcos Douglas <md at delfire.net> wrote:

> Hi,
>
> I'm using RegExpr to parse some parts, just SOME parts, of a HTML and
> works perfectly. But, in one case I need identify a "pattern" to reuse
> using new "values".
>
> Please, see the example (using Lazarus) bellow:
>
> procedure TForm1.Button1Click(Sender: TObject);
> var
>   InputStr: string;
>   r: TRegExpr;
> begin
>   r:= TRegExpr.Create;
>   try
>     InputStr := '<a href="/3604669/trabalho-28-07-2014-pg-1" title="Página
> 1, pg 1, 28/07/2014"> 28/07/2014 </a>';
>     r.Expression := '\/([0-9]+)\/.*?pg\-(\d+).*?"';
>     r.Exec(InputStr);
>     ShowMessage(r.Match[0]); // = /3604669/trabalho-28-07-2014-pg-1"
>     ShowMessage(r.Match[1]); // = 3604669
>     ShowMessage(r.Match[2]); // = 1
>   finally
>     r.Free;
>   end;
> end;
>
> The results are OK.
> I need to replace the position #1 and #2 with something like:
> #p1# and  #p2#
>
> So, in other words, I need a result like this:
> Pattern = /#p1#/trabalho-28-07-2014-pg-#p2#"
>
> I tried to use r.Replace() or r.Substitute() but I think these methods
> don't do I want.
>
> Is possible?
>
> --
> Marcos Douglas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140906/413b2510/attachment.html>


More information about the fpc-pascal mailing list