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 Escapes

gabrielalcaraz
gabrielalcaraz
7,681 Points

Didn't get the right match back. Got "1".

I try to get the match object but I don't know what that means, help?

escapes.py
import re

def first_number(x):
    x = '1234'
    num = re.match(r'\d', x)
    return num

2 Answers

This passes:

import re

def first_number(x):
  return re.search(r'\d', x)
gabrielalcaraz
gabrielalcaraz
7,681 Points

ah i see what I did wrong thanks

I kinda wish the challenges also let the student, not just write the function, but also use it in the code. For example:

import re

def first_number(x):
  return re.search(r'\d', x)

str_num = 'abc1234'

print( first_number(str_num)[0] )

(run with python3)

I think it would help the student better understand the syntax and how it really works.

BTW, I test simple python code snippets here: https://www.katacoda.com/courses/python/playground (If you run that code there it prints 1)

Also, I used this line of code:

print( first_number(str_num)[0] )

and not

print( first_number(str_num) )

because

print( first_number(str_num) )

prints <_sre.SRE_Match object; span=(3, 4), match='1'>

Anyway, glad I could help!