[no audio]
Friends here we are going to
work with Python Regular Expressions with
Flags. See already
we have seen the rules to create your patterns, and these
are the rules which we saw to create your patterns.
[no audio]
Right. Now we are going to work with your Regular Expressions
with flags as well.
So these are the available flags for your Python Regular
Expressions. See these are the flags. You can use either shortcut
or this flag name, both are same. Now, among all those as of
now I am going to work and I'm going to show you with 're.i'
and 're.m'. Later we'll work with the remaining as
well. Now first of all, 're.i' or 're.IGNORECASE'.
You can use either this shortcut or this full-length
flag. Makes the regular expression case-insensitive.
So let me open my editor. To get clarity on this
I am writing a simple script.
[no audio]
'import re' module.
Let me take some 'text=
"this is a
string"', and I'm writing "ThIs
[no audio]
is a new string", something like that.
Then I'm writing simply all are capitals.
[no audio]
Now see that my requirement is, I need to find all the strings
with "this".
"this" string
I need to find. Suppose if I take a pattern as in this way,
'my_pat' as, raw string I'm taking, so that is a good practice.
We just try to take 'r' before writing your pattern. Then
let me print 're.findall'
with your pattern in a given 'text'.
What is the result you are getting? What happened?
[no audio]
Sorry, 'findall'.
We're getting, 'this'. But if you don't consider case sensitivity,
then 'this' is a 'this', and see that you have 'this' here, and
here, and here. But I am looking for small 't-h-i-s'. But they
are combination with small and capital and these are completely
capital. Now,
I don't want to look for case sensitivity, then just include
a flag called 're.I'.
Now see the result.
See here I included a flag.
[no audio]
So you can use either 're.I' shortcut of your flag, or
'IGNORECASE'.
That is also same. See that. Let me rerun and see the output.
So just use a shortcut. That's better.
That's it.
So this is the use of your 'IGNORECASE' sensitivity flag.
Then we have next thing 're.M', 'MULTILINE'. See that. First
of all let me take some multi-line text.
[no audio]
Let me take my 'text' as, so guys
this is useful,
I mean this option, 're.m' is useful whenever if you are
going to work with files using your file object or read operation.
Anyhow we will see that example as well. First
let me write some string with multiple lines.
So, "this is a string",
and, "this is a second line", something like that.
I am writing, and, "This is a", suppose
[no audio]
third line", and this. So if you have a string in this way, first
of all this is a multi-line string. You can print and see the result.
See you are getting output in multi lines.
So we are giving in terms of triple quotation your string,
that means that is a multi-line string.
Now, what I am doing is, I am writing a pattern just to find
'this'. 'print(re.findall)', from your pattern in the given
multi-line text. See the result.
Let me comment this 'print(text)'.
Now see the result. Wherever 'this' is there you are getting that.
Okay. Anyway, I'm looking as of now suppose small 't-h-i-s' string.
Yes, you are getting that. Now,
I want to print if 'this' is there at very first in a given
string. Then I can take '^' symbol, right. See the result.
'this', yes, in very first line you have a 'this' at very
first. You are getting that 'this'. Then what about these two,
eighth line and ninth line you have 'this' string, right?
See, if you remember from our previous rules '^' - Start
of the string and start of the line in case of multiline.
So by default '^' will look only in the first line,
if you have a multi-lines. But we have multi-lines and in
multi-lines we have 'this'. If you want to look in each and
every line, by default '^' won't look. By default '^'
will look only in the first line, but I want to look
in each and every line. Then you need to add a flag called
're.m' or 're.MULTILINE'.
Now see that. In two places at very first we have
'this' in two lines.
So look in all the lines, look in all multi-lines
your Regular Expression pattern.
That is the use of 're.m'.
[no audio]
Now along with your 're.m' I want to ignore case sensitivity
as well because I need to look for this, 'This' also,
I mean ninth line 'This' also. Capital 'T', 'h-i-s'. Then you have to
include your case sensitivity flag as well.
Then you have to use '|', and then you can use 're.I'.
So if you want to work with multi-flags, you have to include
all flags with the '|' symbol.
[no audio]
Right. That's it. The same way you can look for ending words as well.
See, let me comment this.
Let me write here, suppose this. Let me write some new thing
'end', and here suppose I'm going to, I'm writing the 'end'. Now
my requirement is, I want to print 'end', if 'end' is there
at the ending in a given text or string. So pattern is, you know.
Simply 'end', ending you're looking, '$'. Now see the result.
[no audio]
'end', 'end' you're getting. Why you're getting? I included
here 're.m'. Let me remove that.
[no audio]
Now see that. You're not getting 'end' in the, I mean in the multi-lines
You are going to get only in the one line.
Sorry, ending, ending not about this.
This is about this. See the result.
[no audio]
'End of the string', so in end of the string we have only 'end'
here. But in each and every line if you want to look for your
required string is there at the ending or not, then you need
to include multi-line. That's it.
At the same time if you want to include case sensitivity,
let me write here capital 'E-n-d', or capital 'E-n-D'.
That's it.
[no audio]
Okay. See guys, you can get clarity suppose if I take here and
I am removing this multi-line.
[no audio]
You can keep your case sensitivity if you want.
First let me remove multi-line. And see the result.
You are getting only last 'end' because '$' means in
your entire string only last line. '^' means first line
starting, end means last line ending word. But in multi-lines
I have 'end'. I need to find that as well. Then 're.m', and
if you include 're.m', and if you remove 're.I' it won't work,
because these are
some combination with capital and small.
Suppose if I write here small 'end', now it is going to fetch
that because you included 're.m'. But to include this 'EnD'
and this 'enD', you need to write case sensitivity as well.
[no audio]
That's it. Now see the result.
[no audio]
Okay.
Okay, guys, we will see the remaining flags in later videos, okay.
Okay guys, thank you for watching this video.
[no audio]