[Tutor] Regex not working as desired

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 27 03:45:05 EST 2018


On 27/02/18 05:13, Cameron Simpson wrote:

> hard to debug when you do. That's not to say you shouldn't use them, but many 
> people use them for far too much.


> Finally, you could also consider not using a regexp for this particular task.  
> Python's "int" class can be called with a string, and will raise an exception 

And, as another alternative, you can use all() with a
generator expression:

>>> all(c.isdigit() for c in '1234')
True
>>> all(c.isdigit() for c in '12c4')
False
>>>

Or generally:

def all_digits(s):
    return all(c.isdigit() for c in s)

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