Recent

Author Topic: regular Expression Help needed  (Read 1974 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 540
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
regular Expression Help needed
« on: March 19, 2019, 02:37:19 pm »
Hello.
I have this .DMF file, and I wante to locate "Parameters = <> " under the TADOQuery to delete it.
so How I do that.
I have only the idea to use regex.
Code: Pascal  [Select][+][-]
  1.       object stbBottom: TStatusBar
  2.       Left = 3
  3.       Top = 431
  4.       Width = 766
  5.       Height = 19
  6.       Panels = <
  7.         item
  8.           Width = 150
  9.         end
  10.         item
  11.           Width = 50
  12.         end>
  13.       ParentShowHint = False
  14.       ShowHint = False
  15.     end
  16.   end
  17.   object pnlTopInfos: TPanel
  18.     Left = 0
  19.     Top = 0
  20.     Width = 772
  21.     Height = 153
  22.     Align = alTop
  23.     BorderStyle = bsSingle
  24.     TabOrder = 1
  25.     object ImgFond1: TImage
  26.       Left = 1
  27.       Top = 66
  28.       Width = 766
  29.       Height = 82
  30.       Hint = 'FOND1'
  31.       Align = alClient
  32.     end
  33.   object qryBEZZZListe: TADOQuery
  34.     Connection = DMConnect.cntMain
  35.     CursorType = ctStatic
  36.     Parameters = <>
  37.     SQL.Strings = ()
  38.     Left = 400
  39.     Top = 8
  40.     object qryBEZZZListeBEZZZ_CODE: TStringField
  41.       DisplayLabel = 'Equipe'
  42.       DisplayWidth = 12
  43.       FieldName = 'BEZZZ_CODE'
  44.       Size = 10
  45.     end
  46.  

I use regex from http://wiki.freepascal.org/Regexpr.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

CCRDude

  • Hero Member
  • *****
  • Posts: 596
Re: regular Expression Help needed
« Reply #1 on: March 19, 2019, 02:53:35 pm »
What kind of help do you need exactly?

Do you want to remove any parameters, or just empty ones like in your quote? If the former, are you simply looking for the expression Parameters = \<[^\\>]*\> maybe?

BSaidus

  • Hero Member
  • *****
  • Posts: 540
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: regular Expression Help needed
« Reply #2 on: March 19, 2019, 02:58:07 pm »
need to locate a block 
Code: Pascal  [Select][+][-]
  1.   object qryBEZZZListe: TADOQuery
  2.     Connection = DMConnect.cntMain
  3.     CursorType = ctStatic
  4.     Parameters = <>
  5.     SQL.Strings = ()
  6.     Left = 400
  7.     Top = 8
  8.     object qryBEZZZListeBEZZZ_CODE: TStringField
  9.       DisplayLabel = 'Equipe'
  10.       DisplayWidth = 12
  11.       FieldName = 'BEZZZ_CODE'
  12.       Size = 10
  13.     end
  14.  

then in the block I need to locate
Code: Pascal  [Select][+][-]
  1. Parameters = <>
and delete it.

BUT the
Code: Pascal  [Select][+][-]
  1. Parameters = <>
must ne in the block of TADOQuery because perhaps this property can exists for other components.


Thsnk
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: regular Expression Help needed
« Reply #3 on: March 19, 2019, 03:00:13 pm »
Probably only the empty Parameters from object "TADOQuery".

So first step would be to match /: TADOQuery(.*)object/ (Parameters probably always exists for subcomponents)
Next would be to remove "Parameters = <> " from those matches.

Not sure if and how it should be done with one regex.

For help and testing there are some online regex sites.
https://regexr.com/
https://regex101.com/
https://www.regexpal.com/

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: regular Expression Help needed
« Reply #4 on: March 19, 2019, 03:18:11 pm »
You do not need regexpr for that.
Just use the string.parse from sysutils on the Tstrings.text string....
No time now, but will add example later.(easy)
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: regular Expression Help needed
« Reply #5 on: March 19, 2019, 03:49:17 pm »
Maybe?
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, RegExpr, sysutils;
  7.  
  8. function RemoveTADOQueryEmptyParameters(AFileName: string): string;
  9. var
  10.   sl: TStringList;
  11. begin
  12.   sl := TStringList.Create;
  13.   try
  14.     sl.LoadFromFile(AFileName);
  15.     Result := ReplaceRegExpr(
  16.                    '(.+object [^:]+: TADOQuery[^:]+[\r\n]+)(\s+Parameters = <>[\r\n]+)(\s+SQL.Strings = .+)',
  17.                    sl.Text,
  18.                    '$1$3', true);
  19.   finally
  20.     sl.Free;
  21.   end;
  22. end;
  23.  
  24. begin
  25.   try
  26.     WriteLn(RemoveTADOQueryEmptyParameters('your.dfm'));
  27.   except
  28.     on e: exception do writeLn(e.Message);
  29.   end;
  30.   ReadLn;
  31. end.

Explaining the expression:
want to remove
spaces
Parameters = <>
followed by new line marker:

(\s+Parameters = <>[\r\n]+)



It has to be preceded by:
object
anything except a colon
: TADOQuery
other properties (do not include a colon)
new line marker:

(.+object [^:]+: TADOQuery[^:]+[\r\n]+)



*Not sure about this part, depends if this is really the next property always*

And it should be followed by SQL.Strings property:
(\s+SQL.Strings = .+)



The replacement is the 1st group and the 3rd group:
$1$3

the 2nd group is what gets deleted.

There are other methods to approach this problem.
« Last Edit: March 19, 2019, 04:26:44 pm by engkin »

BSaidus

  • Hero Member
  • *****
  • Posts: 540
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: regular Expression Help needed
« Reply #6 on: March 19, 2019, 08:54:38 pm »
 @○engkin

thnks you  :D ;D
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018