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 trialAndrei Oprescu
9,547 PointsI don't quite understand how this works
Hi!
I have watched the video and I had a confusion on this line of code: 'print(re.match(r'Kenneth', data))'
I don't understand how this code would know what to return because the code says that it will return a pattern that it will find. What pattern is that?
Thanks!
Andrei
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsIn this case, the pattern is a fixed string "Kenneth
", so the matched string is the same as the pattern string.
Using re.match
in this manner results in a basic "does string exist in data". Notice the output shows "span={0, 4)" for matching "Love
" and "span=(6, 13)" for matching "Kenneth
". These represent the character indexes of the match. So data[0:4]
would be "Love" and data[6:13]
would be "Kenneth".
Post back if you have more questions. Good luck!!
Andrei Oprescu
9,547 PointsAndrei Oprescu
9,547 PointsHi Chris!
I have read you answer and it was helpful but can you tell me also how does python know where to look in memory to find this pattern? Surely these patterns are based on some criterias.
Thanks!
Andrei
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsThe pattern is built from the first argument, "
Kenneth
" in this case, and is stored internally to there.search
method. The actual internal logic engine or state machine isn't viewable.In words, i would read it as βLooking for a K, followed by an e, followed by an n,.....β and so on.
This pattern is compared to the data by scanning each character in the data for a match the the first pattern character. If it is a match, then compate then next two characters. If the pattern is broken, then then scanning starts again with the next data character.
I hope i understood your question correctly.
Andrei Oprescu
9,547 PointsAndrei Oprescu
9,547 PointsHi again!
What I meant I don't understand is how does the code 're.match(r'Kenneth', data)' Give the result 'Love in return.
I hope you understood my question:).
Thanks!
Andrei
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsAh. It doesnβt match both. In the video, there are two searches, one for r'Love' and one for r'Kenneth'. Each one independently finding the corresponding pattern in the data.
Andrei Oprescu
9,547 PointsAndrei Oprescu
9,547 PointsBut how does this code give us this answer? It could have given us any other word from that .txt file.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsA pattern that consists solely of a string can only imagine exactly that same sequence of characters so no other word could possibly be returned.