Sign in Join

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

Mailing lists / fpc-pascal / 2014-09

RegExpr - replace

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Marcos Douglas

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

Reply by email

Marcos Douglas
to Marcos Douglas
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 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 >

Reply by email

leledumbo
to Marcos Douglas

If I'm not mistaken what you want:

{$mode objfpc}{$H+}
uses
  RegExpr;
var
  InputStr: string;
  r: TRegExpr;
begin
  r:= TRegExpr.Create;
  try
    InputStr := ' 28/07/2014 </3604669/trabalho-28-07-2014-pg-1>  ';
    r.Expression := '(\/)([0-9]+)(\/.*?pg\-)(\d+).*?"';
    r.Exec(InputStr);
    WriteLn(r.Substitute('$1$4$3$2'));
  finally
    r.Free;
  end;
end.

should do it.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/RegExpr-replace-tp5719987p5719989.html Sent from the Free Pascal - General mailing list .

Reply by email

Marcos Douglas
to leledumbo
leledumbo wrote 22 lines
If I'm not mistaken what you want: {$mode objfpc}{$H+} uses RegExpr; var InputStr: string; r: TRegExpr; begin r:= TRegExpr.Create; try InputStr := ' 28/07/2014 </3604669/trabalho-28-07-2014-pg-1> '; r.Expression := '(\/)([0-9]+)(\/.*?pg\-)(\d+).*?"'; r.Exec(InputStr); WriteLn(r.Substitute('$1$4$3$2')); finally r.Free; end; end. should do it.

UAU!
Didn't worked but you gave me the answer because this worked:

{$mode objfpc}{$H+}
uses
  RegExpr;
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.Substitute('$1#p1$3#p2$5'));
 finally
    r.Free;
  end;
end;

I finished this task yesterday making a new method for my own TRegExpr but your tip is more elegant, thank you!

Marcos Douglas

Reply by email