[Tutor] Regex

Alan Gauld alan.gauld at yahoo.co.uk
Tue May 4 09:54:23 EDT 2021


On 04/05/2021 14: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:

This may not be your fault but that of your teacher, but that is a
terrible problem description. Almost impossible to use.

What is a sentence if it does not have a terminator?
It's really just a string.

So your problem is to extract the string beginning with ERROR.

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

So again our description is inadequate. What do we do if there are
parens inside the string before the name?
Is the name always at the end of the line?

For example how do we deal with these two cases:

ERROR blah (de) blah (name)
ERROR blah blah (name) bloop bloop

What should be printed in each case?

If you can precisely define the problem then the code
should be relatively easy. But if you have a terrible
problem description you need to improve it before you
can solve it. Either by asking questions or by making
assumptions. But document those assumptions (in comments?)
so you know how to fix/change them if need be.

> I would have preferred to use grep myself, 

But grep is just a regex recognizer. You still have
to write the correct regex. And grep won't break up
the line to only include the bits you want.

> I decided to use re.sub and remove the parenthese and 
> then use the script I posted before. 

I think that's more complex than need be.

The simplest regex that might match your problem definition
(so far) is:

re.search(r'ERROR[^(]*', string)

Which matches ERROR followed by any number of non '(' characters.
It will include the space so you might want to strip() the result
if you need to lose the space. (Your problem definition doesn't
make it clear if the space is part of the "sentence" or not)

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