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 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 68,423 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!!!