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

Multiple string by int to create a pattern

Hello can someone explain this problem to me and how to be solve im confuse right now.

Thank you

escapes.py
import re

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

def numbers(a, b):
    num = a * b

1 Answer

Robin *
Robin *
5,670 Points
# write a function named numbers
# that takes two args: a count(int) and a string
def numbers(count, string)
    # Return an re.search for exactly x(count) numbers in the string
    return re.search(r'\d' * count, string)

This will search the string for numbers if its reapeated x(count) times i.e if count is 3 it will look for 3 numbers. If for example the string is 'postman pat 123' it will find 123