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

nuri jeon
nuri jeon
14,376 Points

Position of the elements in the set! :D I have a question!

Hello! I was watching video about the set and was wondering about the position of this set [-\w\d+.] So I thought it doesn't require any rule when it comes to putting things(ex -, +, . etc) so I did this ---> [\w\d-+.] And... it didn't work :>..... So is there any rule or reason I have to put dash('-') in front of \w\d? or for + and . ? Thanks for help always!

1 Answer

Steven Parker
Steven Parker
220,682 Points

When you use a hyphen inside a character class, it normally combines with the character in front of it and the character after it to represent a range of characters starting at the first and going to the last. For example, "[0-9a-z]" represents any digit or lower-case alpha character.

By putting a hyphen first (or last), you can cause it to represent only itself instead of combining with the others to represent a range.

Thank you! I was having issues with this myself and this was a great explanation. Perhaps it should be included in the teachers notes.