1
00:00:00,000 --> 00:00:03,990
So let's take a look at how we use regular expressions in Python.

2
00:00:04,000 --> 00:00:07,990
We'll start by making a working copy of regex.py and we'll call

3
00:00:08,000 --> 00:00:12,990
this regex-working.py.

4
00:00:13,000 --> 00:00:17,990
Regex is a common short name for regular expressions, because who wants to say

5
00:00:18,000 --> 00:00:20,990
all those syllables when you can just say regex?

6
00:00:21,000 --> 00:00:26,990
Here we have a very simple program that opens a file called raven.txt, which has

7
00:00:27,000 --> 00:00:29,990
the complete text of Edgar Allan Poe's poem, The Raven.

8
00:00:30,000 --> 00:00:37,990
So we open that file and then we read the file line-by-line and we do a regex

9
00:00:38,000 --> 00:00:43,990
search for anything that matches this pattern here, and this pattern here

10
00:00:44,000 --> 00:00:48,990
basically matches the word Lenore or the word Nevermore, which occur with some

11
00:00:49,000 --> 00:00:49,990
regularity in this poem.

12
00:00:50,000 --> 00:00:53,990
And then it prints them out and you will notice we have end= the blank string,

13
00:00:54,000 --> 00:00:57,990
because we don't want to put a new line at the end of a line that already has a

14
00:00:58,000 --> 00:00:59,990
new line at the end of it.

15
00:01:00,000 --> 00:01:05,990
And so we'll go ahead and run this and there we have the lines that have

16
00:01:06,000 --> 00:01:08,990
Nevermore or Lenore in them.

17
00:01:09,000 --> 00:01:15,990
So searching with regular expressions is done using the re.search method of the

18
00:01:16,000 --> 00:01:20,990
re module and here is our regular expression pattern.

19
00:01:21,000 --> 00:01:22,990
This is a very simple regular expression pattern.

20
00:01:23,000 --> 00:01:27,990
It's beyond the scope of this course to teach you regular expressions, but in a

21
00:01:28,000 --> 00:01:33,990
nutshell what this one does is it's an alternation and so it has the regular

22
00:01:34,000 --> 00:01:37,990
expression alternation operator which you can just call OR.

23
00:01:38,000 --> 00:01:48,990
So anything that has Len or Neverm immediately followed by the letters ore.

24
00:01:49,000 --> 00:01:53,990
So this will match Lenore and it will also match Nevermore and we see in our

25
00:01:54,000 --> 00:01:59,990
results down here that we have lines that have Nevermore, quoth the raven, and

26
00:02:00,000 --> 00:02:02,990
we have lines that have Lenore.

27
00:02:03,000 --> 00:02:08,990
So that's how you do a search using regular expressions in Python.

28
00:02:09,000 --> 00:02:12,990
If you want to, you can just print out the part of this that was matched.

29
00:02:13,000 --> 00:02:18,990
If we take this line here and instead of putting it in the if, we take its

30
00:02:19,000 --> 00:02:26,990
result and call it match, then we get a match object which we can use and this

31
00:02:27,000 --> 00:02:32,990
is some of the beauty of how Python's "everything is an object" helps us out.

32
00:02:33,000 --> 00:02:39,990
We can test match, if we have a match then what we can print here is

33
00:02:40,000 --> 00:02:43,990
match.group(), like that.

34
00:02:44,000 --> 00:02:49,990
So if I save this and run it, now we get all of the words that we actually matched.

35
00:02:50,000 --> 00:02:55,990
Now this can be really useful for simply looking for specific patterns in a set

36
00:02:56,000 --> 00:03:01,990
of text and it can also be used when we look later on for replacing patterns in

37
00:03:02,000 --> 00:03:05,990
text, when we talk about replacing later on in this chapter.

38
00:03:06,000 --> 00:03:16,000
So those are some simple examples of how you use Python's regular expression engine.

