Search code examples
regexterminaliterm2

Regex to capture errors and warnings from g++ and python


I recently converted to iTerm2, and one of the coolest features is the ability to 'capture' regular expressions - useful for catching compiler/interpreter errors and warnings and displaying them in an actionable panel like an IDE.

How would one configure a regex to capture g++ errors/warnings and Python traceback errors? An error thrown by Clang is:

filename.c:54:9: error: use of undeclared identifier 'foo'

which results in the regular expression:

^([a-zA-Z0-9+/.-]+):([0-9]+):[0-9]+: (?:error|warning):

A Python traceback might be of the form:

Traceback (most recent call last):
  File "main.py", line 664, in Run
    self.TriangulatePoints(None, show=True)
  File "main.py", line 570, in TriangulatePoints
    error = gis.triangulate()
  File "~/scripts/terrain.py", line 208, in triangulate
    if int(self.dem_data[ny][nx]) != NDV:
IndexError: index 432 is out of bounds for axis 0 with size 432

and g++:

points_lib.cpp:70:17: note:   initializing argument 3 of ‘void CellsInCircle(int, float, int)’
 extern "C" void CellsInCircle(int maxDiameter, float cellSize, int arrayCellsInCircle)
             ^~~~~~~~~~~~~
points_lib.cpp: In function ‘void PlacePointsIO(double*, double*, int, int, char*)’:
points_lib.cpp:553:56: error: cannot convert ‘std::vector<std::vector<std::vector<int> > >’ to ‘int’ for argument ‘3’ to ‘void CellsInCircle(int, float, int)’
 CellsInCircle(maxEdge, cellSize, arrayCellsInCircle);

Thank you! My regex-foo is nonexistent.


Solution

  • An attempt at a catch-all for errors, warnings and a mention of 'line':

    (?=.*(?:error|warning|line).*).*?(?<file>\w+\.\w+).*?(?<line>\d+)

    (?=.*(?:error|warning|line).*) // look ahead and match one of the words possibly enclosed by white-space
    .*?                            // possibly any white-space
    (?<file>\w+\.\w+)              // filename with 1 char before and after dot
    .*?                            // possibly any white-space
    (?<line>\d+)                   // line as first sequence of digits
    

    Demo