Python - Regex error: 'NoneType' object has no attribute 'group'

Python - Regex error: 'NoneType' object has no attribute 'group'
0

#1

Hi all,

Python newbie here.
I’m following allowing with the book ‘Automate the Boring Stuff with Python.’ I’m working through Chapter 7 where it introduces Regex and the .group method.

I’m writing the code straight from the book:

import re
phoneNumRegex = re.compile( r’((\ d\ d\ d)) (\ d\ d\ d-\ d\ d\ d\ d)’)
mo = phoneNumRegex.search(‘My phone number is (415) 555-4242.’)
mo.group(1)

Yet I get the error: AttributeError: ‘NoneType’ object has no attribute ‘group’.

I’m not sure if I’m missing something or if the .group() method needs to be imported from another module?

Thanks,


#2

I got some help from my MeetUp group, the spaces between the ‘\ d’ was causing the issue for anyone out there that is curious in the future.