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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Returning multiple matches in a string

Hi,

I cannot seem to get how to solve part 2 of the code challenge to return "count" number of digits within a string input. I am unsure of where my code is lacking as match finds 1 or more matches, not just the first instance from what I read.

Is my code failing perhaps because it is looking for consecutive digits?

escapes.py
import re

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

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

3 Answers

Chase Marchione
Chase Marchione
155,055 Points

Hi Dhruv,

  • You'll want to stick with the search method. (You're searching for the place in the string in which you'd find the 'count' amount of numbers in a row.)
  • For this challenge, there is no need to initialize count to anything (or as being of the type integer, if that was that statement's intention), so you'll want to remove that line.
return re.search(r'\d' * count, string)

Hope this helps.

Drew Butcher
Drew Butcher
33,160 Points

CJ Marchione is correct you can just use:

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