Hey
I am trying to understand how the regexpr works in Lazarus. Unfortunately, it does not go very well for me.
I made the program shown in the picture in attachment. In the Edit field, user can write a regular expression and in the first line Memo1 a string that will be examined for matches. After pressing the button, the program displays each match in new line. Everything works. But how can I refer to a specific match? E.g. My regular expression looks like this:
and the text to be examined is just the path to the file.
c:\35\0\123\0\1.2.4\0\11\
My program will display individual folders from this path in the following Memo1 lines. But I would like to be able to refer to a regexpr match at any time and use it for any operation. For example if I press each button at bottom it will put result into Edit field above.
My code looks like this:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls,regexpr;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Edit1: TEdit;
w1: TEdit;
w2: TEdit;
w3: TEdit;
w4: TEdit;
w5: TEdit;
w6: TEdit;
w7: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
regex: TRegexpr;
i:integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
regex:=TRegexpr.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
regex.Expression:=Edit1.Text;
if regex.Exec(Memo1.Lines[0]) then
Begin
memo1.lines.add(regex.Match[0]);
while regex.ExecNext do
begin
memo1.lines.add(regex.Match[0]);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
w1.text:=regex.Match[0];
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
w2.text:=regex.match[1];
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
w3.text:=regex.match[2];
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
w4.text:=regex.match[3];
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
w5.text:=regex.match[4];
end;
end.