Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Name Groups

Regular expression pattern matching.

Not sure what I am doing wrong but I have this name groups regular expression that I cant see where I am going wrong can anyone help?

It will compile but it prints out 'none'

names.py
import re

string = 'Perotto, Pier Giorgio'

print(re.match(r'''
    ^(?P<fullname>[-\w ]+,\s[-\w ]+)$
''', string, re.X))
print(string)
kyle kitlinski
kyle kitlinski
5,619 Points

What is the intended output? the name?

import re
string = 'Perotto, Pier Giorgio'
print(re.match(r'''
    ^(?P<fullname>[-\w ]+,\s[-\w ]+)$
''', string, re.X).group()) #### added .group() at the end
print(string)
>> Perotto, Pier Giorgio
>> Perotto, Pier Giorgio
Jonathan Kuhl
Jonathan Kuhl
26,133 Points

I got it to work by removing the ^...$ that was surrounding the regex:

(?P<fullname>[-\w]+,\s[-\w]+)

Try using https://regexr.com/ when building regex.

1 Answer

@kyle kitlinski

Hi yes it is part of the name groups of the python coding challenge it is supposed to take a variable names that is a re.match() against the string and should provide groups for the first and last names separate

with both methods still not being accepted by treehouse compiler :(