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

Are strings even compatible with re.match?

I know Kenneth showed how to open and read a file, but can you do the same with a string?

escapes.py
import re

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

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yep. re.match() just wants a string to match against. If you remember, when we were using it to search a file, we were really searching a string we made from the file.

The only Escape hatch that matches this is \w, but it matches 'a' instead because the number isn't the first character.