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

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,019 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,137 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.