This community-built FAQ covers the “Compiling and Matching” exercise from the lesson “Natural Language Parsing with Regular Expressions”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Natural Language Processing
FAQs on the exercise Compiling and Matching
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (
) below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply (
) below!
Agree with a comment or answer? Like (
) to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
Hello, so I don’t think I’m completely understanding the point of the compiling and matching. So you compile your regular expression into a regex object, then you use .match() to see if it matches a string? So, in the example, they used [A-Za-z]{4} as their regex and “Toto” with .match. Soo, can’t you just look and see that Toto matches the regex? It doesn’t make sense that you’d use it on a large amount of text, because the documentation says match only gives you the match for the beginning of the string, or I think you can designate beginning and ending indices, but again, can’t you just look in that case?
I, too, am trying to think this through.
From what I could gather reading the documentation .compile() and .match() are not replacements for each other but are used in conjunction with each other. The benefit comes when you will want to use the same regex multiple times in your code. Thus, .compile() creates an object of a certain pattern that you can call multiple times later. E.G. from documentation:
prog = re.compile(pattern)
result = prog.match(string)
Also, I think these answers on StackOverflow are helpful concerning this topic:
Now on to the specific question you asked: why use match?
My guess/intuition would be to use match in cases where I have many single line inputs:
lists of names, emails, places, etc.
I would use match because I am looking at only one line at a time instead of a body of text, for which I would use search.
E.G. I have a company directory and want to filter out a certain set of characters before I apply another operation to it.
However, after further investigation (reading others’ work) it seems there is little benefit to using match():
Whats the argument 0 in the .group() method, i mean i tried .group(1) and it game an error, and i tried the .group() method with no argument and i got the same result as .group(0), can someone explain please
1 Like