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 Negation

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

A small error in code?

Hello guys, I am really into these lectures, they are really good and I am enjoying them, after the OOP this is a bit like relaxation. Anyway, I tested and played around with this code in my own workspace, and found out that there is a small error in the code that Kenneth presented us in this lesson(code about names and jobs). I found out that the code does not reads(finds) the first word in a Job( if we have two words in a job, before the comma, like "Sith Lord" or "Time Lord), code reads only the second word. In order to fix that, I "made" a space in the first set, after \w : \b[-\w ]+,

With this space, we cover also the first word for Jobs that have two words. Besides that very tinny thing, Kenneth is awesome here. I guess that OOP is really a level for itself.

3 Answers

Actually, to correct Kenneth's code in full you should also remove the \b from the begining of the string. This will allow you to match ', Tim'. I believe --don't know for certain-- that an empty string has nos boundaries. So it seems that with \b you are telling RegEx to look for non-empty strings.

The code should be like this:

print(re.findall(r'''
    [-\w ]*,
    \s
    [-\w ]+
    [^\t\n]
''', data, re.X))
Steven Parker
Steven Parker
231,275 Points

The beginning of a string should count as a word boundary, so you probably have some other issue that affects the result.

If you need help finding it, show your code (perhaps by making a snapshot of your workspace and posting the link to it here).

Steven Parker
Steven Parker
231,275 Points

I see now. And the effect of the original is visible on the screen in the video. The original regex specifies "the word followed by a comma", but with the space added it expands it to "any number of words followed by a comma"

So I'd suggest you report this to the staff as a bug, as described on the Support page. They might add something to the Teacher's Notes (and give you an "Exterminator" badge :beetle:)!

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

Hello, thanks for the quick response. Here is my code:

```names = (re.findall(r""" \b[-\w ]+, # find a word boundary, 1+(1 or more) hypnes or characters, and comma \s # find 1 whitespace [-\w ]+ # find 1+(1 or more) hypnes or characters, and explicit spaces [^\t\n] # Ignore tabs and newlines """, data, re.VERBOSE|re.IGNORECASE))

print(names)```

The only difference with Kenneth's code is that I made whitespace after the -\w in the first set. it gave me full results, however in Kenneth's example, in the results, we did not get the "Sith" and "Time" for the job.