Regex - Regular Expression to get a block of text

1

How would a regex be to capture the block of variables declared in a delphi unit?

The closest I got to this was using this var[^=]*[^\)]; but with no success.

var
Variavel1 : string;
Variavel2, Variavel3: integer;
Variavel4,
Variavel5 : THandle;

function Soma(a, b: integer): integer;
var
  Varivel1: integer;
  Variavel2, Variavel3: integer;
begin
  //...
end;

Any solution to identify variables is welcome.

    
asked by anonymous 16.07.2016 / 01:19

1 answer

0

I do not know delphi but from what I understood from logic the variable region starts with var and ends with some other reserved word like function or begin .

Using this concept can be an expression like this:

^var([^~]*?)(function|begin)

The content you want would be in group 1, and group 2 would be to specify terms.

Explanation

  • ^var - should initial with "var"
  • ([^~]*?) - group 1, capture anything other than ~ 0 or infinite times as few as possible - I used ~ because it is not much used, but could be some other.
  • (function | begin) - group 2, should catch the exact sentence, any one of them.

Be working at REGEX101

    
17.07.2016 / 03:34