[Tutor] Failing regex to identify error stack
Alan Gauld
alan.gauld at btinternet.com
Fri Sep 20 04:46:55 EDT 2019
On 20/09/2019 09:09, Asad wrote:
> Hi All ,
>
> I am writing a script in python to read a logfile and identify the first
> error stack :
>
> ?/test/admin/nothing.sql
>
> PL/SQL procedure successfully completed.
> Session altered.
> Package created.
> Session altered.
> Session altered.
> Package body created.
> No errors.
> Session altered.
> PL/SQL procedure successfully completed.
>
> ERROR at line 9:
> Fal-11144: size of object not allowed
>
>
> I am trying to use the following regex :
>
> ^.*/test/admin/.*(?=ERROR at line .*)(?=Fal-.*)
Regex are complex things and hard to debug, I'd probably
use simpler string comparisons and a sentinel.
Something like (untested pseudo code):
inStack = False
stack = []
errorCode = ''
for line in logfile:
if line.startswith('?/test/admin/'): #start stack
inStack = true
continue
if line.startswith('ERROR'): # end of stack
inStack = False
continue
if inStack:
stack.append(line)
else:
errorCode = line
break
You should wind up with stack containing all the lines
between the filename and ERROR and errorCode with
the line after ERROR.
Finally, you could wrap it in a function that returns a
tuple containing stack and errorCode...
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list