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 Sets

Mona Jalal
Mona Jalal
4,302 Points

confusion about \b

so when to use each?

print(re.findall(r'\b[trehous]{9}\b', names, re.I))
print(re.findall(r'[trehous]{9}', names, re.I))

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,094 Points

The regex character \b represents a zero-length position at a word boundary. It is used if you are looking for whole words and reject matches that may be inside other words.

In your example, ypu would you'd the \b to say "I am looking for a sequence of 9 characters from this set but not sequences that are within sequences of 10 or more characters.

This means using \b would reject matching "treehouses" since 9 characters aren't surrounded by word boundaries.