- [Instructor] Next let's take a look at replacing
and splitting sub strings.
Now previously, we've talked about various string methods
that enable you to do searching and replacing and splitting.
But in the context of regular expressions,
you have more power because you can look for complex
patterns to either replace or split larger strings at.
So as you can see, we've already imported the re module.
And let's take a look at our first expression here.
In this case, the expression that we're going
to be looking for is a tab character,
and we're going to be replacing tab characters in the string
that we're going to search with comma spaces.
So every tab will be replaced with a comma and a space.
And this is the string in which we are going
to perform the operation.
So you can see here, we have three tab characters
and if I execute that it returns to me a string
in which every tab has been replaced by a comma and a space.
And if for any reason you have the need to limit
the total number of replacements,
you can use the count keyword argument.
So if I say I only want to replace the first two tabs,
with commas and spaces, I can do that.
In which case, the last piece of the original string
is left in tact.
So that's regular expression substitution,
with the sub function from the re module.
Now let's talk about splitting a string
using the regular expression version of the split function.
So the string type has a split method,
that will operate on whichever string you call split upon.
Here we're passing in the regular expression to search for,
and the string that should be split.
So let's take a look at our regular expression first here.
You can see that we have a raw string consisting of a comma.
Which is a literal value.
Followed by, a back slash s,
which is the character class for white space characters,
and asterisk quantifier.
So what this says is, I'm going to have a comma
followed by zero or more white space characters.
Well if you look at the string
in which we're going to search, in this case we have a comma
followed by two white space characters.
A comma followed by two white space characters,
a comma followed by zero white space characters,
a comma followed by a bunch of white space,
and then a couple more commas
with no white spaces following them.
So this pattern is going to match every case
in which we have commas in this string
and any number of space characters from zero
to as many as necessary to replace all of those commas.
And remove them from the string
to create a list of sub strings.
So when I do that you can see we're able to very quickly
parse out all of the values from this comma separated list
that had awkward spacing within it.
Now just like we did up above, we have a keyword argument
that we can use, that enables us to specify the maximum
number of splits.
It's called max split, and if I set that,
let's say to three.
Then it will only split in the first three matches
of the regular expression and then the remainder
of the string will become the in this case,
fourth element of the resulting list.
So we get one, two, and three,
and then this is the remainder of the resulting string.