Regex - Regular Expression to get a text block

Asked

Viewed 353 times

1

How would a regex capture the block of declosed variables in a Delphi unit ?

The closest I’ve come to this is using this var[^=]*[^\)]; but without 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.

  • What exactly do you expect to return from this example block that you have provided?

  • Only the same variables... would be +- as in this post http://answall.com/questions/77831/regex-para-capture

1 answer

0


I don’t know Delphi but from what I understood from logic the region of variables starts with var and ends with some other reserved word like function or begin.

Using this concept one can make an expression like this :

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

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

Explanation

  • ^var - must start with "var"
  • ([^~]*?) - group 1, capture anything other than ~ 0 or infinite times as little as possible - I used ~ because it’s not much used, but it could be some other.
  • (Function|Begin) - group 2, must capture the exact sentence, any one of them.

Whether working on REGEX101

  • I liked the answer but this expression would have a different effect on version 2 of the regex ?

  • @Mutha version 2? sorry I don’t understand. The difference of yours is that it seeks the minimum, and has a definite end.

  • Excuse my ignorance looking at a material here I imagined that there was a version 2 to regex.

  • Quiet, but good that solved your problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.