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

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?