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

Got no output

While going through the video i tried to do write the same code as the instructor wrote. but as i ran the program, i got an empty tuple. i tried checking the code but couldn't see any difference. here's my code:

print(re.findall(r'''
                 ([-\w ]+, \s[-\w ]+)\t # names
                 ([-\w\d.+]+@[-\w\d.]\t) # emails
                 (\(?\d{3}\)?-?\s?\d{3}-\d{4})\t # phones
                 ([\w\s]+,\s[\w\s]+)\t # job and company
                 (@[\w\d]+) # Twitter
                 ''', data, re.X))

2 Answers

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

HI, Ammar Fatihallah ! Try this:

print(re.findall(r'''
                 ^([-\w ]*, \s[-\w ]+)\t # names
                 ([-\w\d.+]+@[-\w\d.]+)\t # emails
                 (\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # phones
                 ([\w\s]+,\s[\w\s.]+)\t? # job and company
                 (@[\w\d]+)?$ # Twitter
''', data, re.X|re.M))

can you please tell me why code didn't work ? Was it a typo ?

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Ammar Fatihallah , main problems:

  • ^ and $ for start and end of each string and re.M for multiline.
  • in email regex closing parenthese is after \t, which is tab after email.
Daesy Stephens
Daesy Stephens
4,590 Points

change instead re.findall to re.search