Recent

Author Topic: What is the right regular expression to rework a property block  (Read 255 times)

huckfinn

  • New member
  • *
  • Posts: 8
What is the right regular expression to rework a property block
« on: November 19, 2019, 05:29:48 pm »
Hi folks, I try ro rework a property block from by using the regular expression features in the replace dialog of Lazarus 2.0.4

The code looks like that:

Code: Pascal  [Select]
  1.     fScanner:     TStringList;

The expession for the scanning process  is:
Code: [Select]
(^\s+[A-Za-z0-9])|([A-Za-z0-9]+$)
which means:

  - first pattern is from line start until the end if the identifier limited by space righthand side
  - second pattern is the identifier separated by space space lefthand side to the line end

The expession for the replacement:

Code: [Select]
property $1 $2  read f$1 ;
Dispite of using the multiline switch (which scrambels the output a little bit more) or not  I go the strange result:
Code: Text  [Select]
  1.  property     f   read f    f;Scanner:     TStringList;    

IMO it should be:
Code: Pascal  [Select]
  1.  property fScanner: TStringList read fScanner;

Neither the colon nor the semicolon is part of the output deal and the placeholder $1 and $2 are also not in place.

What I'm doing wrong?

Remarks: A Test with the  regexpr unit and the code:

Code: Pascal  [Select]
  1. program TestReplace;
  2.  
  3. uses RegExpr;
  4.  
  5. var S,G1, G2, G3, R: String;
  6. begin
  7.   S:= 'Ident: TType;';
  8.   G1:= ReplaceRegExpr('(^\w+)|(\w+)|(;$)',S,
  9.                  '$1',true);
  10.   G2:= ReplaceRegExpr('(^\w+)|(\w+)|(;$)',S,
  11.                  '$2',true);
  12.   G3:= ReplaceRegExpr('(^\w+)|(\w+)|(;$)',S,
  13.                  '$3',true);
  14.  
  15.   R:= ReplaceRegExpr('(^\w+)|(\w+)|(;$)',S,
  16.                  '$1 $2 BLAH $1 $3',false);    
  17.   writeln('SOURCE: "', S,'"');
  18.   writeln('GROUP1: "', G1,'"');
  19.   writeln('GROUP2: "', G2,'"');
  20.   writeln('GROUP3: "', G3,'"');
  21.   writeln('RESULT: "', R,'"');    
  22. end.  
  23.  

gives more strange results.

Code: [Select]
SOURCE: "Ident: TType;"
GROUP1: "Ident: "
GROUP2: ": TType"
GROUP3: ": ;"
RESULT: "property Ident  read Ident : property  TType read  property   read  ;"

Best Regards Huck
« Last Edit: November 19, 2019, 06:34:32 pm by huckfinn »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5788
    • wiki
Re: What is the right regular expression to rework a property block
« Reply #1 on: November 19, 2019, 08:41:30 pm »
You are missing a + after the first [A-Za-z0-9]
Then you also need to match the part before and after the ":". You do not do that at all

Also your variable already contains the F, so adding it again in the replace may not what you want?

Code: Text  [Select]
  1. ^\s+F([A-Za-z0-9]+)\s+:\s+([A-Za-z0-9]+)\s+;\s+$

But instead you can record an editor macro. https://wiki.lazarus.freepascal.org/IDE_Window:_Editor_Macros
Record the keystrokes needed to rewrite one line (in such a way that each line can be done by the same keystrokes), including at the end going one line down.
Then replay them.

e.g.
Start of line
if needed, ctrl right (next word)
type "property "
if needed del, to get rid of the F (that relies on that ALL vars start with F)
ctrl-C (if you have "copy word on none" set)
go to end of line
one to the left (get before ;)
type " read F"
ctrl v
cursor down (next line, so you can keep playing the macro)
« Last Edit: November 19, 2019, 08:43:48 pm by Martin_fr »

bytebites

  • Full Member
  • ***
  • Posts: 220
Re: What is the right regular expression to rework a property block
« Reply #2 on: November 20, 2019, 12:38:53 am »
Code: Pascal  [Select]
  1. program TestReplace;
  2.  
  3. uses RegExpr;
  4.  
  5. var S,G1, G2, G3, R, pattern: String;
  6. begin
  7.   S:= 'Ident: TType;';
  8.   pattern:='(^\w+):\W+(\w+);';
  9.   G1:= ReplaceRegExpr(pattern,S,'$1',true);
  10.   G2:= ReplaceRegExpr(pattern,S,'$2',true);
  11.   G3:= ReplaceRegExpr(pattern,S,'$3',true);
  12.  
  13.   R:= ReplaceRegExpr(pattern,S, 'Property $1:$2 read $1 $3;',true);
  14.   writeln('SOURCE: "', S,'"');
  15.   writeln('GROUP1: "', G1,'"');
  16.   writeln('GROUP2: "', G2,'"');
  17.   writeln('GROUP3: "', G3,'"');
  18.   writeln('RESULT: "', R,'"');
  19. end.
  20.  

huckfinn

  • New member
  • *
  • Posts: 8
Re: What is the right regular expression to rework a property block
« Reply #3 on: November 20, 2019, 02:45:26 am »
Thank you Martin,   the advise

Text to find: 
Code: [Select]
^\s+f([A-Za-z0-9]+):\s+([A-Za-z0-9]+);$Replace with:
Code: [Select]
property $1: $2  read f$1;
 gives me the expected result.

huckfinn

  • New member
  • *
  • Posts: 8
Re: What is the right regular expression to rework a property block
« Reply #4 on: November 20, 2019, 03:17:27 am »
Hi bytebites, thank you now it works as expected. Here is the full text code.

Code: Pascal  [Select]
  1. program TestReplace;
  2. uses RegExpr;
  3.  
  4. var C, E, S, G1, G2, PP, PD, PI, FD, FI: String;
  5.  
  6. begin
  7.   {Define a class name}
  8.   C:= 'TClassName';
  9.  
  10.   {Define a private field}
  11.   S:= 'fIdent: TType;';
  12.  
  13.   {Define the regular expression}
  14.   E :='^[fF](\w+):\s+(\w+);';
  15.  
  16.   //@DEBUG G1:= ReplaceRegExpr(E,S,'$1',true);
  17.   //@DEBUG G2:= ReplaceRegExpr(E,S,'$2',true);
  18.  
  19.   {Generate the property entry}
  20.   PP:= ReplaceRegExpr(E,S,
  21.         'property $1: $2 read get$1 write set$1(aValue);\n',true);
  22.  
  23.   {Generate the setter definition}
  24.   PD:= ReplaceRegExpr(E,S,
  25.         'procedure set$1(aValue: $2);\n',true);
  26.  
  27.   {Generate the getter definition}
  28.   FD:= ReplaceRegExpr(E,S,
  29.         'function  get$1:$2;\n',true);
  30.  
  31.   {Generate the setter implememntation prototype}
  32.   PI:= ReplaceRegExpr(E,S,
  33.         'procedure '+C+'.set$1(aValue: $2);\nbegin\n    f$1:= aValue;\nend;\n',true);
  34.  
  35.   {Generate the getter implememntation prototype}
  36.   FI:= ReplaceRegExpr(E,S,
  37.         'function  '+C+'.get$1: $2;\nbegin\n    result:= f$1;\nend;\n',true);
  38.  
  39.   //@DEBUG writeln('SOURCE: "', S,'"');
  40.   //@DEBUG writeln('GROUP1: "', G1,'"');
  41.   //@DEBUG writeln('GROUP2: "', G2,'"');
  42.  
  43.   writeln(FD);
  44.   writeln(PD);
  45.   writeln(PP);
  46.   writeln(FI);
  47.   writeln(PI);
  48. end.
  49.  

Result:

Code: [Select]
$ ./TestReplace

function  getIdent:TType;

procedure setIdent(aValue: TType);

property Ident: TType read getIdent write setIdent(aValue);

function  TClassName.getIdent: TType;
begin
    result:= fIdent;
end;

procedure TClassName.setIdent(aValue: TType);
begin
    fIdent:= aValue;
end;
« Last Edit: November 20, 2019, 03:21:46 am by huckfinn »