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 Groups

Can anyone tell me what am I missing? "Same" code written in different way not working.

I was just practicing and messing around with regex tools when I tried to use groups to match only the FIRST LINE of the data.

First I tried this and it worked:

print(re.search(r'''(\w+, \w+)\t(\w+@\w+.\w+)\t(\(\d{3}\)\s\d{3}-\d{4})\t(\w+,\s\w+)\t(@\w+)''', data))

# OUTPUT: got back a Match Object containing the entire first line of the string, witch belongs to all Kenneth's infos.

Then, I tried to make this more readable by using this exact same code, only written in multiple lines as re.VERBOSE allows me to do that. So the code turned into this:

print(re.search(r'''
    (\w+, \w+)\t
    (\w+@\w+.\w+)\t
    (\(\d{3}\)\s\d{3}-\d{4})\t
    (\w+,\s\w+)\t
    (@\w+)\t
''', data, re.X))

# OUTPUT: None

Why am I getting no match? I thought it was supposed to work the same way as my first try did. I would really appreciate some help! It's bugging me a lot.

Thanks!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,762 Points

Again with the challenging questions! When using the re.X switch all regular spaces are ignored. In your first group, there is a blank space after the comma that should be replaced with \s. With this change the regex works fine.

Wow! Chris Freeman as always being a LIFE SAVER! Thank you so much!!!