Python's regular expression module has a way that you can pre-compile a regular expression when you're going to be using it over and over again. This is an efficiency and it also allows you access to some of its more advanced functionalities. So let's go ahead and make a working copy of regex.py, call it regex-working.py. I will open that working copy up and you'll see here that we have a tight little loop that's using the same pattern over and over again and it's using it to search for this pattern, in line after line of text.
So we can get some more efficiency out of this loop by pre-compiling the regular expression. So up here we open the file. We can say pattern = re.compile and then we will come down here and we will cut and paste our pattern and then when we do the search down here, you can say pattern like that. And regular expression module will recognize that that's a pattern object.
That's a precompiled regular expression object. And we will use that, so when we save this and run it. You see we get the same result, but it's more efficient because we are simply compiling that regular expression once rather than otherwise the regular expression module needs to compile the regular expression each time it uses it and this is in a tight little loop and we want that loop to be fast. This also gives us the ability to use some of the regular expression module's other features like for example, you'll notice in the raven text, the very last use of the word nevermore, it's not capitalized at the beginning.
And so we don't have this line, "Shall be lifted - nevermore!" at the end of our results. So we can tell the regular expression model to ignore case, just by putting in re.IGNORECASE. And in fact you can use just an I, like that. I like to use the whole thing because it's more explicit and when I come back to look at it years from now, after maybe I have not used Python for some reason for a while or not used this particular feature, I will remember what it means.
So it's something you just need to type out once and so it's worth the extra effort. So if I save this and run it, now we get that last line as well, because we are telling the regular expression module to ignore case. So a number of efficiencies are available if we are using pre-complied regular expressions. For example, if I want to do a substitution down here, I can say pattern.sub comma line, like that and now I have the efficiency of having a pre-compiled pattern.
I can go ahead and use the regular expression module to do the substitution and I don't necessarily have to get the result out separately and then use the string module substitution. So this allows me to use the regular expression engine for more things without the inefficiencies of having to compile the pattern over and over and over again. So if I save this and run it, now we have that same result and it's easier to read and it flows in with our whole pattern if you're using the regular expression module and we even have that last line with the lowercase nevermore.
It's still working because the pattern was compiled with the IGNORECASE. So that's just some of the efficiencies that you can get from using pre-compiled regular expressions in Python. It's not hard to do. I recommend that you do it that way if you're going to be using your regular expressions in a loop or for any purpose that's more than just once or twice.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98382 Viewers
61 Video lessons · 85669 Viewers
71 Video lessons · 69603 Viewers
56 Video lessons · 101928 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.