Recent

Author Topic: Accessing REGEXPR matches  (Read 2306 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
Accessing REGEXPR matches
« on: March 13, 2018, 08:35:15 pm »
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:
Code: Pascal  [Select][+][-]
  1. [\d.\d]{1,6}

and the text to be examined is just the path to the file.
Code: Pascal  [Select][+][-]
  1. 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:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls,regexpr;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Button3: TButton;
  19.     Button4: TButton;
  20.     Button5: TButton;
  21.     Button6: TButton;
  22.     Button7: TButton;
  23.     Button8: TButton;
  24.     Edit1: TEdit;
  25.     w1: TEdit;
  26.     w2: TEdit;
  27.     w3: TEdit;
  28.     w4: TEdit;
  29.     w5: TEdit;
  30.     w6: TEdit;
  31.     w7: TEdit;
  32.     Memo1: TMemo;
  33.     procedure Button1Click(Sender: TObject);
  34.     procedure Button2Click(Sender: TObject);
  35.     procedure Button3Click(Sender: TObject);
  36.     procedure Button4Click(Sender: TObject);
  37.     procedure Button5Click(Sender: TObject);
  38.     procedure Button6Click(Sender: TObject);
  39.     procedure FormCreate(Sender: TObject);
  40.   private
  41.  
  42.   public
  43.  
  44.   end;
  45.  
  46. var
  47.   Form1: TForm1;
  48.   regex: TRegexpr;
  49.   i:integer;
  50. implementation
  51.  
  52. {$R *.lfm}
  53.  
  54. { TForm1 }
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. begin
  58.   regex:=TRegexpr.Create;
  59. end;
  60.  
  61. procedure TForm1.Button1Click(Sender: TObject);
  62. begin
  63.   regex.Expression:=Edit1.Text;
  64.   if regex.Exec(Memo1.Lines[0]) then
  65.      Begin
  66.     memo1.lines.add(regex.Match[0]);
  67.       while regex.ExecNext do
  68.     begin
  69.       memo1.lines.add(regex.Match[0]);
  70.     end;
  71.      end;
  72. end;
  73.  
  74. procedure TForm1.Button2Click(Sender: TObject);
  75. begin
  76.   w1.text:=regex.Match[0];
  77. end;
  78.  
  79. procedure TForm1.Button3Click(Sender: TObject);
  80. begin
  81.   w2.text:=regex.match[1];
  82.  
  83. end;
  84.  
  85. procedure TForm1.Button4Click(Sender: TObject);
  86. begin
  87.    w3.text:=regex.match[2];
  88.  
  89. end;
  90.  
  91. procedure TForm1.Button5Click(Sender: TObject);
  92. begin
  93.   w4.text:=regex.match[3];
  94.  
  95. end;
  96.  
  97. procedure TForm1.Button6Click(Sender: TObject);
  98. begin
  99.     w5.text:=regex.match[4];
  100. end;
  101.  
  102. end.
  103.  
  104.  



engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Accessing REGEXPR matches
« Reply #1 on: March 14, 2018, 04:23:52 am »
This expression [abc] means *one* letter: a or b or c.
This expression [\d] means *one* digit: 0 or 1 or ... or 9.

Repeating the letters of digits inside the brackets does not change the meaning. Both of these expressions are identical:
[\d] and [\d\d]
same for:
[abc] and [abdabd] or even [aabbccccc]

This part of your expression [\d.\d] is equivalent to [\d.] which means one digit or one period.
The rest of the expression {1,6} specifies how many times to be repeated.

The result of your expression when applied on c:\2018-03-22\ is:
Quote
2018
03
22

Does not seem like what you want.

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Accessing REGEXPR matches
« Reply #2 on: March 14, 2018, 06:40:01 am »
Yeah I figured it out just after posting it, that my expression whas too long though it worked for me. But I  need only folders names that contain digits and/or combination of digits and dots. So expression. [\d.]{1,6} works for me. {1,6} is for limiting folders with max 6 characters.
And I think that I know why I can't access each match separately. I always put each match in the same index 0. I think I need to add incrementing variable for each found match. Maybe then I will be able to access each match.
Or Maybe I need to put each match into stringlist and then I could use TStringList.Strings[index] to access each match... hmmm
« Last Edit: March 14, 2018, 08:53:30 am by TomTom »

 

TinyPortal © 2005-2018