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

write a function named numbers() that takes two arguments: a count as an integer and a string. Return a match for exactl

write a function named numbers() that takes two arguments: a count as an integer and a string. Return a match for exactly count numbers in the string. Remember, you can multiply strings and integers to create your pattern.

Not returning number, please help =)

escapes.py
import re
def first_number(s):
  return re.search(r'\d', s)


def numbers(count=5, string):
  return re.search(r"\w" * 5, string)

5 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

You had it mostly right, but I think you got carried away by what the example is saying

For example: r"\w" * 5 would create r"\w\w\w\w\w".

what this challenge is asking for actually isn't all that different than the previous one

def numbers(count, string):
  return re.search(r"\d" * count, string)

that was frustrating at first since I thought you did the exact thing as me, just in different format... now I see the escape character

Adam Oliver
Adam Oliver
8,214 Points

I don't understand why I'm not getting the correct answer back. It says 'auo' returned?

import re

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

def numbers(count, my_str):
  return re.match(r'\w' * count, my_str)
Adam Oliver
Adam Oliver
8,214 Points

Thanks. I also was fooled by the 'match' in the objective and added that to my code instead of search.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It's hard to talk about because what's returned is literally a Match object, whether it comes from re.match() or re.search().

on the second return you are using ...r'\w'... which says you are looking for unicode characters... you want a different escape character instead of w