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

Anish Walawalkar
Anish Walawalkar
8,534 Points

Python regexp problem. re.search(), didnt get the correct match

dont know exactly what the problem with my re.search() is in numbers()? Question: Write a function numbers() that takes 2 arguments, a counter and a string, returns a search object containing the count # of characters

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

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

It shouldn't be \w - it should be \dlike this:

import re

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

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

2 Answers

Hi Anish,

The pattern needs to match numbers only, not alphanumeric characters.

  • Return a match for exactly count numbers in the string

Look at your search pattern in numbers:

+What does \w return?

+What is the question asking you to return?