[Tutor] error message

Alan Gauld alan.gauld at freenet.co.uk
Mon Feb 14 02:37:56 CET 2005


> string = 'My phone is 410-995-1155'
> pattern = r'\d{3}-\d{3}-\d{4}'
> re.match(pattern,string).group()

> AttributeError: 'NoneType' object has no attribute 'group'

When match doesn't find anything it returns None, which has no 
group() method.

Why does it not find the regex?
Because you used match() which looks for a match starting 
at the beginning of the line. You need to use search() 
instead...

I discuss this in my tutorial topic on regex...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


More information about the Tutor mailing list