1
00:00:00,000 --> 00:00:02,990
Let's take a look at how we can do search and replace using regular

2
00:00:03,000 --> 00:00:04,990
expressions in Python.

3
00:00:05,000 --> 00:00:13,990
Start by making a working copy of regex.py. Call it regex-working.py.

4
00:00:14,000 --> 00:00:18,990
We'll open our working copy and here we have our little script that searches for

5
00:00:19,000 --> 00:00:20,990
a particular pattern in a file.

6
00:00:21,000 --> 00:00:26,990
If we run this, you see it finds all the places where either Lenore or Nevermore

7
00:00:27,000 --> 00:00:30,990
is found in Edgar Allan Poe's "The Raven."

8
00:00:31,000 --> 00:00:34,990
So let's do a search and replace instead of just a plain search.

9
00:00:35,000 --> 00:00:40,990
Search and replace in Python is done with re.sub, so we'll change this re.search

10
00:00:41,000 --> 00:00:45,990
to re.sub and we'll use the same pattern there.

11
00:00:46,000 --> 00:00:50,990
And we put in our replacement pattern here. We'll just replace it with some hash

12
00:00:51,000 --> 00:00:58,990
marks and we are going to turn this if into just a print and we'll at the end of that,

13
00:00:59,000 --> 00:01:04,990
we'll put the end= blank like that and we don't need this anymore.

14
00:01:05,000 --> 00:01:11,990
So now, we have re.sub and sub is the search and replace in the regular

15
00:01:12,000 --> 00:01:14,990
expression package in Python and the first argument is the regular

16
00:01:15,000 --> 00:01:16,990
expression pattern itself.

17
00:01:17,000 --> 00:01:20,990
The second argument is what to replace it with when it's found, and then the

18
00:01:21,000 --> 00:01:23,990
string to replace it from.

19
00:01:24,000 --> 00:01:27,990
Now in this case, this will go ahead and print the string even if it doesn't

20
00:01:28,000 --> 00:01:28,990
find any replacement.

21
00:01:29,000 --> 00:01:30,990
So it will print the string either way.

22
00:01:31,000 --> 00:01:34,990
So we'll get the entire file with just those lines replaced.

23
00:01:35,000 --> 00:01:38,990
So I'll save this and run it and we'll go ahead and maximize this and we can see

24
00:01:39,000 --> 00:01:43,990
these places where the hash marks are found. These are places where it found

25
00:01:44,000 --> 00:01:49,990
either Lenore or Nevermore.

26
00:01:50,000 --> 00:01:53,990
So that's the simplest form of a search and replace in Python.

27
00:01:54,000 --> 00:01:56,990
Let's say though that we just want to print the lines where we actually found it

28
00:01:57,000 --> 00:02:00,990
and print those lines with that string replaced.

29
00:02:01,000 --> 00:02:04,990
So one easy way to do that is instead of putting the print here we can do a

30
00:02:05,000 --> 00:02:09,990
search here and get this results of the search with match, hence we use this

31
00:02:10,000 --> 00:02:14,990
search function, just how we learned to do it before.

32
00:02:15,000 --> 00:02:23,990
So search in line like that and then if match, because we only want those lines

33
00:02:24,000 --> 00:02:28,990
whether it's been a match, then we can do a simple string substitution, and

34
00:02:29,000 --> 00:02:40,990
print line and say end= blank like that and we can use the replace method in the

35
00:02:41,000 --> 00:02:43,990
line object itself.

36
00:02:44,000 --> 00:02:49,990
And we have our result in match and it's in the group method and what we replace

37
00:02:50,000 --> 00:02:51,990
it with is the hash marks.

38
00:02:52,000 --> 00:02:56,990
So this is actually doing the search and replace in two separate steps.

39
00:02:57,000 --> 00:03:01,990
The search is being done using the regular expressions and the result is in the match object.

40
00:03:02,000 --> 00:03:06,990
We can test the match object to see if it's true, then it will have a match and

41
00:03:07,000 --> 00:03:13,990
then we can use that match in the string replace method to do the replacement.

42
00:03:14,000 --> 00:03:18,990
So if we save this and run it, so now we get only those lines where the string

43
00:03:19,000 --> 00:03:22,990
was found and we have the replacement in those lines.

44
00:03:23,000 --> 00:03:25,990
So those are a couple of simple ways to use Python's regular expressions module

45
00:03:26,000 --> 00:03:36,000
to do search and replace.

