[Tutor] Regex
P L
la_spirou at hotmail.com
Tue May 4 07:15:56 EDT 2021
I forgot to mention the assignment asks to try to solve via regex and I don't know the length of the string. It's a large system log file and I have to parse through each line and extract the sentence that follows the word ERROR. Sorry for not mentioning that.
________________________________
From: Tutor <tutor-bounces+la_spirou=hotmail.com at python.org> on behalf of Alan Gauld via Tutor <tutor at python.org>
Sent: May 4, 2021 6:19 AM
To: tutor at python.org <tutor at python.org>
Subject: Re: [Tutor] Regex
On 04/05/2021 10:51, Alan Gauld via Tutor wrote:
>>>> s = "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add
> information to closed ticket (mcintosh)"
>>>> start = s.index('ERROR')
>>>> end = s.index('ticket')+len('ticket')
>>>> s[start:end]
> 'ERROR Tried to add information to closed ticket'
>>>>
Following up my own post...
find() would be better than index() here since it returns -1 if not
found - which will work on the slice to give the whole remaining string...
Also I should have included start in the second search:
start = s.find('ERROR')
end = s.find('ticket',start)+len('ticket')
s[start:end]
--
Alan G
Author of the Learn to Program web site
https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.alan-g.me.uk%2F&data=04%7C01%7C%7C21773837b7914c3f347408d90ee63852%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557204210358462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=NxRj61z2bFU%2B2BIhADhAt%2BiPBPEcW%2Fm7Ge9xc%2BvvtP0%3D&reserved=0
https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amazon.com%2Fauthor%2Falan_gauld&data=04%7C01%7C%7C21773837b7914c3f347408d90ee63852%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557204210358462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jyBj68gHiQxw1buFAwLU%2F8UUlUbIIWdv0d2Sm4yraaI%3D&reserved=0
Follow my photo-blog on Flickr at:
https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Falangauldphotos&data=04%7C01%7C%7C21773837b7914c3f347408d90ee63852%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557204210358462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=K2MWegIG0nT54eqG1GsJ3R%2BdolXPvX3ia2gRfIyOWGs%3D&reserved=0
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=04%7C01%7C%7C21773837b7914c3f347408d90ee63852%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557204210358462%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=M%2FCR4dRjA2Vi2RCKEfjf1iGt%2BYZE5Ep0ZDYBmUZ1gJ0%3D&reserved=0
More information about the Tutor
mailing list