I have an error log of a few thousands line and I would like to match all the occurences of these type:
Traceback (most recent call last):
File "my_code.py", line 83, in upload_detection_image
put_response = s3_object.put(Body=image, ContentType="image/jpeg")
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(*args, **params)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 676, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.
More in general, I would like to match some text starting with a given string, and ending with another string. The text could be multiline. The problem is that the beginning and end of the text could contain some dedicated regex characters that might require to be escaped. My attempt:
pattern = rf"Traceback(.*){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}",
re.findall(pattern, text, flags=re.MULTILINE)
It gives me error on findall
:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 2, in <module>
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 300, in _compile
raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern
Thanks
All of your comments contribute to the final solution:
pattern = rf"Traceback(.*?){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}"
results = re.findall(pattern, text, flags=re.DOTALL)