Reputation: 131
I have a string containing a Python stack trace like this (with some irrelevant text before and after):
Traceback (most recent call last):
File "/workspace/r111.py", line 232, in test_assess
exec(code)
File "a111.py", line 17, in
def reset(self):
File "/workspace/r111.py", line 123, in failed
raise AssertionError(msg)
AssertionError: Dein Programm funktioniert nicht. Python sagt:
Traceback (most recent call last):
File "a111.py", line 6, in
File "/workspace/r111.py", line 111, in runcaptured
exec(c, variables)
File "", line 1, in
ZeroDivisionError: division by zero
Now I want to extract the line in which the error occurred (1
extracted from File "", line 1
) using a multiline RegEx (in Ruby).
/File ".*", line ([0-9]+)/
works nicely, but matches all occurrences. I only want the last. Iterating over the matches in the target environment is not a valid solution, as I can't change the business logic there.
Upvotes: 4
Views: 1351
Reputation: 626738
You may use
/(?m:.*)(?-m:File ".*", line ([0-9]+))/
Details
(?m:.*)
- a modifier group where the multiline flag is on and the dot matches any char including line break chars that matches any zero or more chars as many as possible up to the last occurrence of the subsequent subpatterns(?-m:File ".*", line ([0-9]+))
- another modifier group where the multiline flag is off and the dot now matches any char but line break chars:
File
- a literal substring with a space after it".*"
- a double quote, any zero or mmore chars other than linebreaks and then another double quote, line
- comma, space, "line" substring([0-9]+)
-Group 1 capturing one or more digits.Upvotes: 5