[no audio]
Friends here our concept is
'compile' operation or method of 're' module
See we know that we have different types of operations in
your 're' module.
Let me go with some of them so that I can explain the usage
of 'compile' operation. Suppose
I have some string here, now
I need to write a pattern such that I want to find 'python'
is there a given string or not.
Then just let me write a pattern.
Suppose first I will go and work with 'findall' operation.
How many 'python' are there in a given string?
So for that I'm writing simply a pattern. See that, first
I'm writing simply 'python'. But I want to look for a word,
then you have to write with '\b' starting and ending, then
exactly word matching. Now, first
let me simply 'print(re.findall)', from your pattern
in a given string how many matchings are there.
See the result. You're getting only one 'python', but we have
here 'python', and here also 'python'.
Anyway, these are 'python2' and 'python3'. No problem.
So here capital 'P', and here small 'p'.
Then you can write in two ways to identify this small 'p' 'python',
capital 'P' 'Python', that is
if you write in square brackets, very first character
it maybe small 'p', or capital 'P', [pP]. Now see the result.
[no audio]
Or instead of that, instead of that suppose
if you don't want to consider about case sensitivity simply
you can write a flag here. If you remember there is an option called
'flags'. Simply write 're.I'.
Now see the result. You're going to get. 'python', small and
capital. Now, along with that
I need to identify 'python2' or 'python3' also. Then at the
end 2 or 3 is optional, right. Then there is a '?'.
So the '?' meaning is either 2 or 3,
maybe once or none,
that is the meaning of your '?'. See we already
discussed about the rules to create your patterns in our
previous videos, right?
So based on that I am creating this pattern. That's fine.
Now, let me run this and see the result. Now you're going
to get 'python', capital P 'Python', 'python2', and 'python3'.
So that is the usage of patterns actually,
I mean 'regex', Regular Expressions.
That's fine. Now,
suppose I am using different operations on my same pattern,
suppose I am using 're.search',
[no audio]
your pattern, then in a given string. So what is your given
string, this one. Now see the result.
You're getting some object.
Yes, there is a match.
So if there is a match, then you can look for your required,
I mean, what is the match and where is the matching position
occurred in a given string, you can go with that like, 'start',
'end', 'group', right,
we have those operations. That's fine.
Now, let me do one more operation.
So I want to print, I want to split my given string based
on my pattern.
So what is my pattern? So normally on your strings you have
a split operation, if you go with that you can able to split your given
string based on one single string, but here I am going to
divide my given string based on either 'python' or capital 'P'
'python' or 'python2' or 'python3', simply wherever these four
words are there in a given string there split will happen.
So that is the usage of 'split' from your Regular Expression.
Now, let me write your string and see the result.
Yes. So likewise, you can use your different operations.
Right. But here, suppose sometimes you are going to use same
pattern multiple times in your given code,
I mean, whatever the code you're writing, in your code
you are going to use your pattern,
whatever the pattern you have that pattern you are going to
use multiple times.
So whenever if you are using multiple times,
instead of using in this way
we are going with 'compile' operation. First see the code so
that you can understand then I will explain you once again.
Now, I am creating pattern object, pattern object.
Nothing is there. Simply 're.compile',
whatever the pattern you have. That's it.
Now if you print your pattern object.
[no audio]
First see the result.
What happened?
[no audio]
Sorry, spelling mistake.
Yeah, see that. You are getting a pattern but in terms of object.
Now, this is a object not like simply your 'python', something like
that. Your 'Python' is converting now your pattern into an
object. Now on this object instead of performing your operations
in this way 're.search', 're.findall', 're.split',
or whatever the operation you have, instead of that directly
you can perform your operation on this object.
[no audio]
See that. Sorry, 'pat_obj', 'print', suppose
'pat_obj.', your operation,
then only string because already pattern is there in this
object, only string, your string.
[no audio]
Now see the result. Whatever the output previously you got
same output you're getting here.
Let me enable
your previous output, without 'compile' operation whatever
the way we are using to use 'search',
[no audio]
is there any difference between these two, first output and
last output? There is no difference.
But while comparing first procedure, I mean sixth line syntax,
your 13th line is having more efficiency.
So speed of operation is very high if we use simply pattern objects.
[no audio]
Not only for that, you can use all operations. Now,
let me disable your normal direct operations.
Now I am using, you can use, for each and every operation you
can use this pattern object.
'pat_obj.findall', then simply your string.
What is the String? Only pattern is there in this object guys.
So now this pattern object is supporting to perform your
all Regular Expressions without any 're' module.
See that. Previously to perform your operations you're using
're.', but now we are using 'pat_ob.'. So already
inside of this 'pat_ob', your 're' is there and
your 'compile' pattern is also there. Now directly we are doing
our operation on that. See the result.
'pat_ob.findall("python"). What happened?
Where is it? Yeah.
See that. Previously
we used ignore case sensitivity, now in 'compile' operation
you can also use flags then only you can get your remaining
pythons, because we are looking for capital 'P', right. Now see
that 'flags=re.I'. So 'compile' operation or method
is also supporting 'flags' operations. Now see the result.
[no audio]
That's it. So guys, nothing is there. Let me write one simple, suppose
if you are using 're.findall', your pattern, then your
string, this we are doing in this way.
[no audio]
're.compile()', your pattern, '.findall'. This is exactly equal to this thing.
I mean any operation you can compare in this way.
So 're.findall', now
we are doing 're.compile'. First
we are creating an object using your patterns, on that object
we are performing our operation.
You can also store this output into some variable.
That's what I did.
[no audio]
Right. Nothing is there. See most of the people will get confusion
why we need to use 'compile' operation? Simple, whenever if
you are using same pattern in different places in your code,
then instead of in that way just try to compile your object,
try to create 'compile' object first. On that object
you just try to perform your required operation in
this way, so that your code is efficient, speed of operation is high.
Other than that nothing is there.
Okay. Okay guys,
thank you for watching this video.
[no audio]