[Tutor] Regex

P L la_spirou at hotmail.com
Tue May 4 13:33:01 EDT 2021


Super, re.search(r"ERROR.*?) \(",s).group(1) was exactly what I was looking for.

Thanks again, apologies for not making it clear!

Cheers

________________________________
From: Tutor <tutor-bounces+la_spirou=hotmail.com at python.org> on behalf of Peter Otten <__peter__ at web.de>
Sent: May 4, 2021 9:59 AM
To: tutor at python.org <tutor at python.org>
Subject: Re: [Tutor] Regex

On 04/05/2021 15:15, P L wrote:
> The assignment is to search for the word ERROR and the sentence that follows it. There is no terminator to the sentence, simply a blank space, as the following:
>
> Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)

Well, if the sentence is guaranteed to be followed by a space and an
opening parenthesis you can use that as the "terminator":

 >>> s = "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add
information to closed ticket (mcintosh)"
 >>> import re
 >>> re.search(r"(ERROR .*?) \(", s).group(1)
'ERROR Tried to add information to closed ticket'

Alternatively, with a lookahead assertion:

 >>> re.search(r"ERROR .*?(?= \()", s).group()
'ERROR Tried to add information to closed ticket'
_______________________________________________
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%7Cddad8692c30a49d9ea4f08d90f04e069%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557335875222875%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=EeNpJMadBfNkQnN7%2FcIWc4iVuAiZ8gCD10bXzgKFrB4%3D&reserved=0


More information about the Tutor mailing list