[Tutor] Regex

Alan Gauld alan.gauld at yahoo.co.uk
Tue May 4 06:19:34 EDT 2021


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
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