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

General Discussion

REGEX Headache

I'm reading up on regular expressions, and understanding everything, until I hit this:

^ is a line anchor outside a class, but a class metacharacter inside a class (but, only when it is immediately after the class's opening bracket; otherwise, it's not special inside a class)

Boink! My brain twisted, and suddenly I'm lost.

I can't wait until Treehouse tackles regex! You folks will explain it in a way I understand.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

So, yes, my course is coming out soon (but it's regex in a Python-centric explanation, of course) and you've been waiting 10 months, but I want to answer this for you anyway.

re.search(r'^Hello', 'Hello there')
'Hello'
re.search(r'^hello', 'Well, hello there')
None

So, outside of a set (where did you find the 'class' name? That's a weird one to me), the ^ means "This is the beginning of the string". The opposite of this, $, means "This is the end of the string".

Now for example #2.

re.search(r'[^2468]+', '0123456789')
'013579'

We're using a set [ ] of characters that we want to match. But since we started the set with a caret, that means "Don't match this set!".

Does that help, 10 months too late (in my defense, I've only been here for about 8 months)?

Kenneth, thanks! I'm going to tackle this again once your course launches.