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 Email Groups

Zoltan Onody
Zoltan Onody
7,491 Points

What is the difference between these 2 regular expressions?

Hey, I have these 2 different regex...

1) (?P<email>[-\w\d.\+]+@[\w\d.]+)

2) (?P<email>[\w\d.-\+]+@[\w\d.]+)

The first is working properly but the second throws "bad character range" error, why? I thought that the order of characters don't matter.

1 Answer

In the second one you'll need to escape the "-". It's attempting to create a range from \d - \+ which doesn't make sense; typically this is use for things like [0-9] or [A-Z]. And you'll need to escape the "." as it will attempt to match any character thus making your character set kind of useless.

Edit: As Kenneth Love pointed out below "." in a set doesn't match any character, it only matches periods. Edited to remove bogus info.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

. in a [set] does not attempt to match any character, it only matches periods.

Eep! My bad Kenneth Love!