Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Patterns, sets, groups, oh my! Now that we have so much control over what our patterns find and what we can create from the Match
es we get back, let's look at a way to turn regular expressions into a variable and then loop over our results.
New terms
-
re.compile(pattern, flags)
- method to pre-compile and save a regular expression pattern, and any associated flags, for later use. -
.groupdict()
- method to generate a dictionary from aMatch
object's groups. The keys will be the group names. The values will be the results of the patterns in the group. -
re.finditer()
- method to generate an iterable from the non-overlapping matches of a regular expression. Very handy forfor
loops. -
.group()
- method to access the content of a group.0
or none is the entire match.1
through how ever many groups you have will get that group. Or use a group's name to get it if you're using named groups.
-
0:00
Eventually, you're going to be in a situation where you need to
-
0:02
use a pattern on 100 different items in a big loop.
-
0:05
Or maybe it'll be 500, or 1000.
-
0:08
You don't wanna have to build and match the pattern every single time.
-
0:11
So the regular expression library has given us a way to compile a pattern into
-
0:15
an object that we can match against.
-
0:17
Let's look at how to do that.
-
0:18
We've already seen that we can make patterns as strings.
-
0:21
We stored them as a variable back, way, way back in the first video.
-
0:25
But, that's not really the most useful thing in the world.
-
0:27
I mean like, if we have, say this string, yeah, it's handy having this as
-
0:32
a variable, but it doesn't save us a lot of time or trouble.
-
0:36
It'd be better if we could say that pattern in a state where it
-
0:40
was ready to go.
-
0:41
It's ready to be used as a regular expression.
-
0:44
And we can do that.
-
0:46
That's what the Compile method let's us do.
-
0:49
So that's actually change line here.
-
0:52
Instead of Search, we'll wanna say Compile.
-
0:55
And what that does is this is gonna take the regular expression and compile it,
-
0:59
get it ready for use.
-
1:01
Now the one other thing we have to change, is we have to take out where it says data.
-
1:06
Because when we compile a regular expression,
-
1:08
we don't compile it with the data it's gonna be run against.
-
1:11
It's like making the regular expression a bit more generic.
-
1:13
We can now run it against a lot of different things.
-
1:16
Not just the one thing that we did the search against, or the match or whatever.
-
1:21
So, I'm gonna take out this line one and let's look at this one.
-
1:24
So we've got line.groupdict and
-
1:26
that no longer makes sense because line isn't a match.
-
1:29
So let's go ahead and do a match.
-
1:31
We'll do re.search, line, data, and then groupdict.
-
1:39
So what we did here, is we created a regular expression search,
-
1:43
just like before, and we said, okay, your pattern is this compiled one, it's line.
-
1:49
And the string you're gonna match it against is data.
-
1:51
And now notice, we didn't specify any flags.
-
1:54
You specify the flags when you do the compile, not when you do the search.
-
1:59
So let's save that and let's Run.
-
2:03
And there we go, we got that same thing.
-
2:06
So, that's great.
-
2:07
But what's really, really, really cool,
-
2:10
at least I think so, is we don't have to do this re.search stuff.
-
2:14
What we can do, instead, is we can just use line directly.
-
2:19
[BLANK_AUDIO]
-
2:23
So we can just say, okay, take line.
-
2:25
It's a pattern. We know it's a pattern.
-
2:27
Do a search with it against data.
-
2:31
So let's take that out.
-
2:33
Run this again, and we should get the exact same content, and we do.
-
2:37
So, that's pretty cool.
-
2:39
But, we've only got one thing.
-
2:41
We're only, we're only getting my line.
-
2:42
How do we get the rest of the lines?
-
2:44
Well, this is the last part of the r-a library that I wanna go over with you.
-
2:49
But there's still more in there to explore.
-
2:51
Go check the docs.
-
2:52
They're awesome.
-
2:53
It's a method that's named finditer.
-
2:55
And it gives us back an iterable of each non-overlapping match.
-
2:59
It's kind of like giving us back a list, but it's not exactly a list.
-
3:02
It's also kinda like using Find All, but instead of getting back topples,
-
3:07
we get back a match object, like when we use re.match or re.search.
-
3:11
So let's try this out.
-
3:13
Let's come back up here and let's say for
-
3:18
match in line.search, oops sorry.
-
3:22
Not line.search.
-
3:24
Line.finditer against data.
-
3:29
We want to print match.group.
-
3:32
Name.
-
3:33
So, this .group method,
-
3:36
when you have a match object says, show me whatever is inside of the group.
-
3:41
Now, I can say, group with a, with nothing, and
-
3:44
it'll show me the whole thing that it captures.
-
3:47
I can do group with a number, and it'll show me the group at that index.
-
3:50
So, 0, 1, 2, so on.
-
3:52
Or I can give it a name and it'll show me the group at that name.
-
3:56
All right, so let's try that out.
-
4:00
So there we go.
-
4:01
There's everybody's names.
-
4:03
You can see Ryan Carson, The Doctor, Exampleson, Example, so on.
-
4:06
All right, that's all of our names.
-
4:09
We can of course ask for any group that we want or do all sorts of other stuff.
-
4:14
For example, let's make a fully qualified email to headers.
-
4:17
You, you've seen this before where it's like a name and then less than,
-
4:21
their email address and greater than, right?
-
4:23
Okay, so before we do that,
-
4:25
I want to actually come back and edit our pattern, and add two things.
-
4:30
So what's cool is we can have subpatterns, or subgroups, sorry.
-
4:34
So we can do here last, and that's the last name.
-
4:42
And we can do here first, and that of course is the first name.
-
4:49
So there's our first name, there's our last name, as these little sub patterns.
-
4:52
'Kay?
-
4:53
So now in our.
-
4:55
For loop, we need to print out a new thing.
-
5:00
And let's print out first, last, email.
-
5:09
'Kay.
-
5:10
And we wanna format this.
-
5:12
With the match.groupdict.
-
5:17
Right? It'll go ahead and
-
5:19
find those keys in the groupdict, it'll use those as keyword arguments.
-
5:26
All right, so let's try this out, we should have email addresses for everybody.
-
5:30
Check it out, we've got email addresses.
-
5:32
There's the name, and an email address.
-
5:35
That works pretty well, and it's a lot simpler than I
-
5:38
would have actually expected it to be if you were just like hey,
-
5:41
turn this into a bunch of email addresses.
-
5:43
Hopefully this will give you some ideas on how to take our address book further.
-
5:47
You can build all sorts of things out of dictionaries.
-
5:50
Made from strings through regular expressions.
-
5:53
In this course, compiling our pattern doesn't actually save us a lot of time or
-
5:56
memory, but it's a good habit to get into, so
-
5:59
you won't forget to do it when it actually matters.
-
6:01
And using things like finditer helps us to save even more memory.
-
6:06
Compiling patterns also lets you make patterns available for
-
6:08
import in other parts of your applications, or even in other packages.
-
6:12
This way, you can make your pattern as perfect as possible in just one place and
-
6:16
reap the benefits anywhere else that you need it.
-
6:18
I think we're done.
-
6:20
We've gone over pretty much every area of regular expressions in Python, and
-
6:24
we've turned a block of text that no one would want to wade through into super
-
6:27
useful dictionaries that we can put into classes or transform into a new text file.
-
6:32
In fact, that's your extra credit for this course.
-
6:34
Take the regex we wrote and the text and make a class for
-
6:37
a person to find by the data.
-
6:39
Give them names and a phone number and an email address.
-
6:41
Maybe make another class for an address book that collects all these
-
6:44
people together and let's the user search them.
-
6:46
Be sure to share it on the forums.
-
6:48
Thanks so much for being part of this course.
-
6:50
I'll see you next time.
You need to sign up for Treehouse in order to download course files.
Sign up