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 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
231,248 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.