Search code examples
delphiexceptiongrepgexperts

How can I find all empty try ... except blocks with GExperts grep?


In new versions of GExperts, the grep utility now supports more 'expert' expressions.

I have not yet found a way to locate empty try ... except blocks in Delphi sources using regular expressions, how could I do this with the GExperts grep tool?


Solution

  • I doubt that GExperts Regex functionality allows you to search beyond line delimiters.

    If you don't mind using a component like TPerlRegEx, following code should get you started to roll your own search.

    var
      emptyExceptBlock: TPerlRegEx;
      Results: TStringList;
    
    emptyExceptBlock := TPerlRegEx.Create(nil);
    emptyExceptBlock.RegEx := except\s+((//.*|/\*.*\*/|\(\*.*\*\))\s+)*end;
    emptyExceptBlock.Options := [preExtended];
    emptyExceptBlock.Subject := LoadFromFile('YourFile.pas');
    Results := TStringList.Create;
    if emptyExceptBlock.Match then begin
        repeat
            Results.Add(emptyExceptBlock.MatchedExpression);
        until not emptyExceptBlock.MatchAgain;
    end;