Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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