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 Groups

Daniel Petrov
Daniel Petrov
3,495 Points

Is there a difference between space and \s?

Is there a difference between using space and \s in the sets and outside? In the name we use [-\w ] and in job we use [-\w\s]. Can we use just space when specifying the space between the words or only \s?

2 Answers

Steven Parker
Steven Parker
210,935 Points

:point_right: A space just matches a space. The \s code matches any kind of "white space", including space, tab, vertical tab, return, new line, and form feed.

Daniel Petrov
Daniel Petrov
3,495 Points

Thanks! What about 2 x space? Is it matched by \s? In this case, it is probably safer to always use \s. Unless you are 100% sure there will be strictly one space.

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,810 Points

Daniel Petrov, yes, \s should match almost any kind of whitespace, even letterspaces, tabs, halftabs, newlines etc. Negation 6:00 And, i guess, if you add + it will match 1x and 2x space and more: \s+