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 Escapes

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)

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!