[Tutor] Regex

Richard Damon Richard at Damon-Family.org
Tue May 4 10:07:26 EDT 2021


It sounds like what you want is a Regex that first matches the token
ERROR, then has a capture group to catch a sequence of words, and then a
terminal ( (that is not in the capture). Maybe if some messages don't
have the (username) add alternatives to the terminal. You then use
.group on the results of the match to extract the capture.

On 5/4/21 9:15 AM, 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)
>
> That's one line from many, with varying sentence lengths and we're not supposed to print the usernames in the parentheses, or the parentheses itself. I would have preferred to use grep myself, but have to use regrex.
>
> I decided to use re.sub and remove the parenthese and then use the script I posted before. I just don't think that's the best approach, seems inefficient. Feels like I can just use re.search but can't find the regex to describe it.
>
> re.sub(r" ?\([^)]+\)", "", error)
>
> Pat
>
> ________________________________
> 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 8:10 AM
> To: tutor at python.org <tutor at python.org>
> Subject: Re: [Tutor] Regex
>
> On 04/05/2021 12:15, P L wrote:
>> I forgot to mention the assignment asks to try to solve via regex
> Ok, I assumed a work assignment not a homework assignment...
>
>> ... extract the sentence that follows the word ERROR.
> That's a very different problem from the one you originally presented.
> In that case the search is for any one of a number of sentence
> terminators - ie any of '.','!','?' (more... in your example string
> the 'sentence' does not have a terminator!)
>
> But the regex should still be relatively straightforward if it starts
> with ERROR and ends with one of the above terminators. Only the
> terminating string changes from 'ticket' to a character group.
>
> But note that the example you posted (ending in 'ticket') does not match
> your new description of the problem. 'ticket' is not the end of the
> sentence.
>
> Half the problem in developing software is in getting the
> specification exactly correct. Once you have that the solution
> often effectively writes itself. Can you post a full and specific
> description of what you are trying to do?
>
> --
> 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%7Cfbb38651382f4becda0608d90ef5b9b3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557270802984314%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=9IDEpuBENHsQwVHbT9JueHZYUz72IQVKF%2Fa1W2tdTLw%3D&reserved=0
> https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amazon.com%2Fauthor%2Falan_gauld&data=04%7C01%7C%7Cfbb38651382f4becda0608d90ef5b9b3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557270802984314%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=vVt6M63k%2B6FDHlW9Cq5i1DTy%2FitC36oxDG9tT2y7rKE%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%7Cfbb38651382f4becda0608d90ef5b9b3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557270802984314%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=FXFxPEgAcZuqnV5WWM5pSY41rDqnzdBEUNk2Fq4cTPI%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%7Cfbb38651382f4becda0608d90ef5b9b3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637557270802984314%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=YGtdZCSXkGNjGNVdRoN1g4aFxbhyLf%2FvkrobB9QKFy4%3D&reserved=0
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


-- 
Richard Damon



More information about the Tutor mailing list