Python Forum

Full Version: AttributeError: 'NoneType' object has no attribute 'group'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

My code for finding a certain text in a larger text doesn't work. It should give: " Phone number found: 415-555-4242". What am i doing wrong?

import re
phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
phoneNumRegex=re.compile(r'\d\d\d-\d\d-\d\d\d\d')
mo=phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group())
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    print('Phone number found: ' + mo.group())
AttributeError: 'NoneType' object has no attribute 'group'
There is a missing \d in the second regex.
Just remove line three.
import re
phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
#phoneNumRegex=re.compile(r'\d\d\d-\d\d-\d\d\d\d')
mo=phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group())
Output:
Phone number found: 415-555-4242
Problem solved, thanks!