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

Ryan Fadholi
Ryan Fadholi
5,168 Points

Why the + sign in the set doesn't have to be escaped?

Hi, I'm under the impression that if I want to match anything that is an operator in regex I need to escape it using '\', for example when we was looking for the '(' the \ is added before the (.

So, any reason why the + sign inside the set in the email regex isn't escaped and it still works?

2 Answers

You can refer https://docs.python.org/2/library/re.html under section [] point 3 where it is mentioned : '(', '+', '*', or ') - will lose their special meaning inside []

Greg Kaleka
Greg Kaleka
39,021 Points

This is the correct answer. Here's the quote:

Special characters lose their special meaning inside sets. For example, [(+*)] will match any of the literal characters '(', '+', '*', or ')'.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello,

do you mean something like this:

r'(\d+)/(\d+)/(\d+)'

if you were trying to match dates in the format dd/dd/yyyy for example (I do not know precisely what part of Kenneth's tutorial you are referring to, so I am making my own example).

you have to use the \ to escape special characters, these include the ( and ) because these define capturing groups in a regex search and will be treated as such if you do not escape them. the + is a special character in regex, it is a quantifier telling the module to look for one or more instances of what comes before it, in the example above it says look for groups of one or more digits. if you escaped it with +, the program would look for + in the thing you are searching through.