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

What is \b actually used for in Python regex?

About 3:30, he used \b at the beginning and ending of the raw string, and I don't understand why doesn't he directly use r''' @[-\w\d.]* [^gov\t]+ ''' to search the data.

Data

Love, Kenneth kenneth@teamtreehouse.com (555) 555-5555 Teacher, Treehouse @kennethlove McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse Arthur, King king_arthur@camelot.co.uk King, Camelot Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik , Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey Exampleson, Example me@example.com 555-555-5552 Example, Example Co. @example Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44 Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers Vader, Darth darth-vader@empire.gov (555) 555-4444 Sith Lord, Galactic Empire @darthvader Fernández de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.

I try to test it and got the result.

['@teamtreehouse.com', '@kennethlove\nMcFarland, Da', '@teamtreehouse.com', '@camelot.co.uk', '@norrbotten.co.se', '@sverik\n, Tim', '@killerrabbit.com', '@teamtreehouse.com', '@ryancarson\nD', '@tardis.co.uk', '@example.com', '@example\n', '@us.', '@potus44\nChalkley, Andrew', '@teamtreehouse.com', '@chalkers\n', '@empire.', '@darthvader\nFernández de la ', '@spain.']

with the \b at the beginning and ending

['@teamtreehouse.com', '@teamtreehouse.com', '@camelot.co.uk', '@norrbotten.co.se', '@killerrabbit.com', '@teamtreehouse.com', '@tardis.co.uk', '@example.com', '@us.', '@teamtreehouse.com', '@empire.', '@spain.']

I check the python official document knows that \b is the boundary that separate \w and \W or \w, but it didn't make sense on the above result.

Any idea?

2 Answers

Hi Brady, I found this on you tube, great help. https://www.youtube.com/watch?v=bgBWp9EIlMM

\b is used to represent a "word boundary." I'm not sure which video you're on, but I'm guessing that word boundaries are important in this case.

For example, if you have the string " abc def ", there are spaces around the abc and the def. There's this "invisible character" in-between the each space and its neighboring letter. This invisible character is the word boundary. Let me make the boundaries stand out using pipes (|s):

 |abc| |def| 

If there were no spaces, there'd be no boundaries:

abcdef

Of course, word boundaries aren't actually there, it's just an abstract way to represent that "this is the beginning/end of a word"