Friends, we have been working with the rules to create your
pattern. Now already
we have seen some of the rules to create your pattern.
Like these are the first set of rules to create your pattern,
and these are the second set of rules to create your pattern.
Now, we are going to work with some more rules.
Now let me take the very first rule,
is curly braces inside that some number.
So what is the meaning for this is, exactly two times.
Now, let me open my editor to get clarity on this usage.
So I have saved this script as 'rules_3.py'.
First of all, let me import my 're' module.
Now just assume that we have a text in this way.
[no audio]
"This is a python".
[no audio]
and python3".
Just assume that. Now what is my requirement
is, suppose if I take my pattern as,
so guys I am using from
now onwards 'raw string', simply 'r'.
Suppose if I try to look for 'python', or even let me remove this.
[no audio]
Now let me 'print()',
[no audio]
your 're.findall()', your pattern in a given text.
Now see the result. You're getting 'python' two times, but in 'python'
at the ending I have 'n' two times. I want to get that, so what I
I can do? I can write 'n' two times. Now, I am going to get that. Yes.
Suppose I have a 'python',
just some character four times then I want to get this entire
matching, then see that. Suppose
I want to look for a 'python' such that 'n' is four times,
I want to look for a 'python' word such that 'n' is four times. If
it is a word you should write '\b' before, on starting. Do
we have that? No, nowhere we have 'python'
with two 'n' word, but we have a word with 'python', 'n' four
times. Then let me write 'n' four times.
[no audio]
Now see the result. Yes, you are getting. But actually here 'n'
is four times no. No need to write four times your 'n', simply
write 'n', then ''.
Now the meaning for this is 'n' is four times. That's it.
[no audio]
Likewise you can apply for any character.
Let's say, suppose
let me comment this pattern.
I am writing somewhere suppose simply 'aa'
[no audio]
then something
then in this way.
Now here also suppose I have 'a', just assume that
three times 'a', here also three times 'a'.
[no audio]
Now just based on our previous rules and current rules
what I want to do is, I want to get a word which consists
of 'a' three times.
[no audio]
Then first of all I am trying to find the word. Whenever
there is a word you have to write '\b' at the starting
and ending. Then between that
you're looking for a word called 'aaa'.
Yes, you are getting that. Only one time you are having.
But 'a' is three times no, why should we write 'a' three times,
simply you can write 'a', in curly braces
you can also write three, ''. That's it.
Now I want to look for a word such that 'a' is 1, 2, 3, 4, 5, 6, 7, 8;
the word which is having,
[no audio]
the word with 'a' eight times. That's it.
Suppose sometimes you may have starting suppose some 'xyz'.
Now you don't have word with 'a' eight times.
You have a word with 'xyz', after that 'a' eight times. Then before
'a' what we have? 'xyz'.
Now see the pattern what I have taken, 'xyz', after that 'a' eight
times. That type of word I want to find.
That's it.
So that is the use of curly braces with the number. Then ''.
[no audio]
See here, if I write it '', exactly two times. If I write '',
then whatever the character before this curly braces you
have, that character maybe two times, or maybe three times,
or maybe four times.
See that. What I am,
let me take a new text.
I will do one thing, I will comment this.
[no audio]
Suppose let us say I have a text called
[no audio]
'xaz', something,
'xaaz',
[no audio]
Then let's say.
[no audio]
'xaaaa', sometimes, 'z', then 'x', 1, 2, 3, 4, let me take three 'z', then 'x'.
Let me take four times also, no problem.
Now our understanding is, now I want to find a word such that
the word should have starting letter is 'x' and ending letter
is 'z'. First
let me write pattern for that.
First of all I'm looking for a word, that's why
'\b \b', starting and ending you should have space, and then my word
should have starting letter as 'x', and ending letter as 'z',
but between this I have 'a'. Now, I need to look for a word
such that it is 'xaz'.
Right. First, let me search in this way.
[no audio]
'findall', your pattern, in your text.
Yes, 'xaz' you are getting.
[no audio]
Now my requirement is,
between these two characters 'a' maybe
one time, or two times, or three times, then simply after 'a'
you have to write one time, or two times, or three times, or
four times. That's it.
Now see the result.
If you have 'x' after that, between these two characters
if you have 'a' one time, yes, it is going to fetch that. If 'a'
is two times, it is trying to fetch that.
Do we have that? No. Here
we have, but here this character, 'z' character is two times.
Now, let me remove one 'z', so that you can get that as well. See that.
[no audio]
That's it. So that is the use of your curly braces.
So exactly for number of times you can write curly braces
with that number, may be one times or two times or three times
before your character,
sorry the character whatever you are looking, then after your
character you have to write in curly braces '',
two or three or four times.
[no audio]
Then two or more times.
I don't know how many times 'a' is there between your two characters,
but I want to search minimum with the two letters, minimum
with two times 'a', and then maximum any number of times.
Now see the result.
Here two times is there, and here some number of times is
there, here some number of times is there. All are going
to fetch because two times or more than that, three times, or
four times, or five times, or infinite times between your 'a' and 'z'.
Sorry your 'x' and 'z' your 'a' maybe two times, or
three times, or four times, or infinite number of times. That's it.
[no audio]
Now '+', one or more. See forget about '+' from this concept
if I want to try to look for one or more times
I can write '1,' in curly braces, ''.
See that
'my_pat' is,
[no audio]
I am writing 'my_pat' as 'xa', 'a'
maybe one time or more than that,
then 'z'. Now see the result.
You're going to get all your words which are having
between 'x' and 'z', 'a'.
[no audio]
'1,'
curly braces with '1,', ''. But same thing you can also
write simply by '+'.
Guys, whatever the symbol we're using, I am using here, that is
for your previous character.
This 'a' maybe one or more times, right. Be clear, 'a' is one or
more times.
That's it.
[no audio]
'a' is one or more times, but same thing you can also write
in this way also.
[no audio]
Both are same.
[no audio]
Now '0 or more times'. See '*' means 0 or more times. Suppose
if I take any word, which is having 'xz'. There is no 'a', right. As of now
if I try to look with 'xa', or let me write
same thing with '+' as well,
'xa+z'. Now see the result.
Same output, but my intention is between your 'x' and 'z', 'a' may
or may not be.
Maybe 0 times means there is no 'a', or maybe more than that.
I mean 0 times or more than, more times, maybe one time,
two times, three times, or four times, then simply
you can take 'a*'. Now see the result. You're going to get
this matching as well for that.
So guys if I write in this way the possible patterns are 'xz',
because '*' means 'a' may be 0 times or your 'a' is one
time or your 'a' is two times, or your 'a' is three times, likewise.
That is the meaning for '*'. '+' means except this first,
remaining all are for '+'.
Then the last one is 'once or none', '?'. Maybe one
time or none time. Not for 0 times or more than. 0 times
or one time, that is for square brackets. Now see that. Here
'a' is there one time between 'x' and 'z', here 0 times it is there, but here
we have multiple times. But if I go with
I'm creating pattern
as raw string, r'xa?z'. That 'a' maybe 0 times,
or one time, once or none.
That's it. Now see the result.
Whichever is having 'a' one-time or 0 times, those things
we are getting. That's it.
[no audio]
So the possible words for this is, 'x', '?' means 0
times, 0 times 'a', 'z'. Then 'x', one time maybe 'a'. That's it.
These two are the possible words.
Now these two words
are there in a given text here and here. So that's why it is fetching them.
That's it.
[no audio]
Right. So guys
these are the some more simple rules to create your pattern,
and these are very, very important.
[no audio]
See whatever the rule we have here,
and this rule we are using in your pattern, but make sure
that whenever if you are applying these rules, these rules
are applicable for your particular character, not for your entire words.
See I am using for 'a' this '?' means, this symbol,
this rule is applicable for this 'a'. Somewhere suppose
we used here '', now for this 'n',
not for your entire word, be clear, not for your entire word,
this is for only 'n' character
you are applying this concept.
[no audio]
Right. Okay guys, thank you for watching this video.
[no audio]