[no audio]
Friends, here we are discussing
about 'split', 'sub', 'subn' operations
of your 're' module.
Let me open my Python terminal, and there I will explain. See
anyway they are very simple operations compared to your
'search', 'match', 'findall', 'finditer'.
Let me open my python terminal, and let me import 're' module.
So first operation is suppose here
we want to discuss about 'split' operation, and if you remember
while starting our Regular Expressions we have started
with 'split' operation only, I mean while giving introduction.
Anyway, here I want to show you some advanced syntax of your
'split' operation.
So simply write 'help(re.split)' operation, and see the
result. We are getting some syntax, right. Now, first
let me take 'my_srt' as,
so, "This is about python and python is very easy
[no audio]
and we are having
[no audio]
python2 vers and python3 vers". Just assume that
that is the string. Now suppose I want to split the given string,
directly I'm splitting. So here is itself
I am writing a pattern.
Otherwise, you can also write your pattern,
let's say, 'my_pat' as,
[no audio]
so I want to divide my string wherever
'python' is there.
Right. Now simply use 're.split' operation, your pattern,
and your string, now see the result first.
See wherever 'python' is there,
there split is happening.
But here this is capital 'P', 'Python'.
But if you observe here this is not simply 'python',
'python2', and 'python3'.
Now, what I want to do is, wherever 'python2', or 'python3', and
with 'python', then there I want to do a split. Now,
if you observe your pattern is going to represent 'python',
'python2' and 'python3', but of course here you are having
capital 'P', so that you can ignore by using, by applying flags.
Now see that, I am applying a 'flag',
[no audio]
case sensitivity, 'flag=', don't consider case-sensitivity,
ignore case-sensitivity. Now see the result. Wherever 'python' is
there, or 'python2' is there, or 'python3' is there, there
split is happening.
Right. So the only thing is, you have to know how to write a pattern,
where you want to split, in which places you want to split,
based on that places
you need to write a pattern. Already we have gone through the
rules to create your pattern.
And one more thing, here if you observe here, 'maxsplit' is
also there, right. Now,
let me run this with 'maxsplit' as well.
[no audio]
Let me copy this. Directly you can write. First
I'm applying 'maxsplit=0', and see the result.
There is no change whether if you write 0, or I mean 'maxsplit=0',
or if you neglect this option there is no change.
But if I write 1, 'maxsplit', split only in one place, that
too from left to right, the first occurred place. Now split
in two places, that too while coming from left to right,
this is left to right, right, left to right.
So they're divided in two places. Now see the result. Here
and here it is splitting. Now
here and here
it is not splitting.
So that is the simple operation by using your 'split' operation.
Right. Then we have 'sub'. Now,
let me show you syntax for your 'sub' first. See the
result. Pattern, I mean whatever the pattern you want, in this
pattern whatever the word you want to replace, then your
string, then count, then flags.
Anyway, you know the usage of flags.
Let me write simply,
anyway you have your pattern, right.
What is your pattern?
[no audio]
Let me write simply, your pattern.
So 'python [23]', right. Now,
what I am doing is, simply 'print(re.sub, in place
of your patterns - 'python', or 'python2', or 'python3',
I want to replace with suppose 'jython',
in your given string.
See the result.
[no audio]
Wherever 'python' is there, there we are replacing.
But here it is not replacing because it is capital 'P'. Then
apply 'flags' to ignore your case sensitivity.
[no audio]
Now, all places it is replacing. But the thing is, it is not
going to disturb your original string. Be clear. You know
the string properties.
So in that case, in case if you want to store the result,
then you can take 'my_new_str',
I mean you can take any variable, and in that you can store it.
Now just print your
new string and see the result. That's it.
Right. And if you observe here, 'count', 'count' means in how many
places you want to replace.
Let me directly print by applying your 'count'. 'count=0',
see the output first. There is no change whenever if you write
'count=0', or even if you skip that option 'count=0'.
But if you write 'count=1', see, only the first place
where you are occurring your pattern, there it is replacing with 'jython'
That's it. But if you write 2, see whenever if you write 2, in 2 places
replace my pattern with required string. This is your pattern,
and this is your required string,
and replace in two places only, that too while coming
from left to right. That is the preference.
See here and here it is replacing. That's it.
Then what about you are 'subn' operation.
You have one more operation, right?
Nothing is there. It's very simple.
Just let me run, and see the result.
It is going to show you your result as a tuple,
and it is giving you a resultant string, and here you are getting
a value called 2 means, in 2 places you are replacing. Anyway,
manually you are mentioning in 2 places replace, but you don't
know in how many places you have your required pattern
in a given string. Just observe that I am removing 'count=2'
Now wherever your pattern is there, in all places you are
going to replace with 'jython'.
But how many places you have your required pattern in a given
string, that it will give. Now see the result, in four places
actually you have your required pattern in a given string, and it
is replaced in four places with 'jython'.
That's it. That's what it is giving.
So this value is not depending on your 'count' value.
But if you write 'count=2', it will replace with only
two, that means you're going to get value 2. But if you don't
use 'count' option in your 'sub', then in all places it
is going to replace, but how many places it has been replaced,
that value will be given by using 'subn' operation
of your 're' module.
That's it. So Guys,
these are the simple ways to use your 'split', 'sub', and
'subn' operations of 're' module.
Very, very useful. Please try to do practice on this.
Okay guys, thank you for watching this video.
[no audio]