Friends, here our concept is
'findall' and 'finditer' operations of 're' module.
So let me explain this with a simple script.
Let me open my editor.
[no audio]
So here let me import 're' module first.
I have given some script name. Now
let me take 'my_str' as suppose.
[no audio]
"This is python and we are having
[no audio]
python2 and python3 versions".
[no audio]
Now, let me take 'my_pat' as, so actually when you are going
to take pattern based on your requirement. Suppose
my requirement is I need to find out either 'python', or 'python2',
or 'python3'. See how can I write? 'python',
[no audio]
So if you are strictly looking for 'python' word or 'python2'
'python3' words, then you have to take '\b' then 'python'.
So our requirement is 'python' or '2' or '3', that's why in
square brackets just write '[23]', then your '?'.
'?' means once or none. Then '\b'. That's it.
Now it is going to represent three words - 'python', 'python2',
and 'python3'. Among these three, right, if you go with suppose
're.MATCH', you know what is the usage of 're.MATCH',
so 're.MATCH(my_pat)',
then your string.
[no audio]
See the result. What you are getting? You are getting 'None'.
The reason is, 'match' will always look at very first in a given
string for your given pattern.
We don't have any 'python', or 'python2', or 'python3' at very
first, that's why 'match' is not able to find that. Now if I
go with the 'search', then you are going to get information
about 'python' because the very first, anyway you know that,
'search' is going to look for your given pattern in a given string
from left to right. While going from left to right
we have somewhere 'python',
so among these three patterns - 'python', 'python2', and 'python3' here
you've got, your 'search' got 'python'.
That's why it is giving that. But my requirement is, I need
to find all the matchings for your pattern which are there
in a given string.
[no audio]
Right. So in a given string, 'python'
is there, 'python2' is there, and 'python3' is also there.
I want to find all these three.
[no audio]
Then our operations are, your 'findall' operations.
Let me print with 're.findall' your pattern. Your pattern
may represent one string or multiple strings, right, so that
I am looking in a given string. Now see the result. You're
getting all the matchings for your given pattern. Suppose
if I take 'pyx'. Now, you don't have any matching for your
pattern in a given string. You're getting a empty list. Be clear.
'findall' will always give you a list.
If there is a match it will give the list of values, if
there is no match it
will give empty list. Now by using 'findall' operation directly
you can tell that how many matchings are there for your given
pattern in a given string simply by applying a 'length' function
on your 'findall'. See the result.
If there is no matching directly it will give 0.
[no audio]
Because we know that for list we can apply 'length' function,
and from that you can find out how many matches are there. That's it.
So that is the use of your 'findall', or basically 'findall'
is useful just to understand 'regex' operations.
But if you go with your 'findall' we have a small drawback,
of course directly we can able to find how many matches are
there for a given, for your given pattern in a given string,
but yeah, let me take this as 'python'.
Yeah, now see that. But the disadvantage is it is not giving
any information about your matching.
I mean, what is the starting index of this matching,
what is the ending index of this matching.
So whenever if you want to look all the matchings in a given
string for your given pattern, and if you want to see at the
same time the starting index or ending index of your matching,
then you have to go with the 'finditer' operation.
Let me print first the information for your 'finditer',
'my_pat,my_str'.
First observe the output what you are getting.
Let me comment your remaining 'print' statements.
See the output. You are getting
'callable_iterator object at', something, some information
is there. Just observe that. Now I am writing 'py', some, let's
say 'pyxthon'. Now, we don't have any matching for your
given pattern because it is not 'python', 'pyxthon'. For this
related word we don't have anything in our given string. Now if I look
in this way, see the result what you are getting? Still you
are getting some object.
So first of all, 'finditer' will give always some object. Whether
there is a match or not, whenever if you run 'finditer' it
will give first some object. You need to remember this point.
Then, see that I am using for loop for this. 'for each
object in this pattern,
I mean in this operation, let me print each object. Now, see the result.
You're not getting anything, because first of all whenever
if you run 'finditer' operation it will give some object
whether there is a match or not secondary. If there is a match
this loop will repeat. If there is no match you're not getting
anything. Be clear.
See in your 'match' and 'search' operation you are using 'if' condition.
If your object is present then what is the match,
what is the starting index,
what is the ending index.
But if you go with 'finditer' in order to use 'if' condition,
because first of all 'finditer' for multiple matchings directly
use for loop. If there is a match then loop will repeat. If
there is at least one match your loop will repeat
at least one time. If there are no matches then your loop,
your loop won't repeat for at least one time.
[no audio]
Now see that. I am making now my pattern as 'p-y-t-h-o-n'. Now see
the output. You're getting three objects.
Suppose I am taking my pattern as only 'python2',
be clear 'python2' or 'python3', now see the result.
Your loop is repeating only one time and it is giving you
object. Right.
So be clear.
Now, I have given my pattern as 'python', in square brackets '[23]',
then '?'. Now see the result, three objects
it is giving. So 'finditer' will consist of the number of
objects based on how many matches are there for your pattern
in a given string. And whenever if you use for loop for that
iterator it will give the different objects for each
match. Right. Now see that. Now I can directly write,
what is the match.
Let me write some message.
[no audio]
"The match is: ",
[no audio]
so here I will print the match.
What is your match?
'.group', right. See that.
[no audio]
See that. Then I want to find what is the starting index for
each and every object.
So "starting index",
your 'each ob.start()', if you remember from your
'match' and 'search' operation. Then what is the ending index.
Be clear, ending index is nothing but your object,
let me write it, your object, '.end() - 1'. Be clear.
Don't forget about that. That's it.
[no audio]
Right. So guys, this is the usage of 'finditer'.
So in most of the cases in your real time, you're going to
work with 'search' and 'match' operations, but you have to know
the functionality or working operation of your 'findall'
and 'finditer' as well.
Sometimes you may use, based on requirement you will use, but
based on my experience
I am telling that most of the cases we'll use 'search' and 'match'.
[no audio]
Okay. Okay guys,
thank you for watching this video.
[no audio]