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

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,810 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,810 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,272 Points

change instead re.findall to re.search