[no audio]
Friends here we are going to discuss about
what are the rules to create
a pattern, such that you can use that pattern in your Regular
Expressions. See already
we know simply what is the RegEx or Regular Expression. See
the 'regex' is a procedure in any language to look for a specified
pattern in a given text.
Now if you want to work with the Regular Expressions using
your Python, then you have to use a module called 're',
and that is the default module.
So if you want to work with 're' in your scripts you have
to import that. Simply 'import re'. Now in this 're' module you
are having different types of operations.
Now, I am going to show you some of them like, search, match,
finditer, findall, sub, split, compile, right, and
see as of now
we are at very beginning stage to work with your Regular
Expressions, that's why I am going to show you simple syntaxes
for your 'search'.
'match', 'finditer', and 'findall'. See
this is one Regular Expression,
and here we are using 'search' operation.
Now the purpose of 'search'
operation from your Regular Expression is different, same
way the 'match' is different, 'finditer' is different, find all is different.
But whatever it may be all these operations are going to
use a called 'pattern'.
Now before going to work with your Regular Expressions operations,
first of all we should be good with 'pattern',
I mean how to create a 'pattern', or
what are the rules to create your 'pattern'.
Right. Once if you are good with pattern, then you can very
easily use your required operation from your RegEx or 're' module.
So first I'm showing you very simple examples for your pattern.
And already we know that guys, what is a pattern?
Pattern is simply a sequence of characters which represent
multiple strings. Now see that I am taking one of the pattern
called "Python" inside of a quotation.
First of all this is a string, your pattern, but this is
normally a string, but whenever if you use this string
inside of your Regular Expressions, then it is going to become a pattern.
Now generally, I am saying that pattern is going to represent
multiple strings.
But of course sometimes your pattern is also going to represent
only one string, no problem.
But if you use your string inside of Regular Expressions,
then this is going to become a pattern. Now this pattern is
going to represent only one string,
but if I take this pattern "python", in square brackets 2,
and 3, '[23]'. First of all, this is a string. Normally this is
a string, but if you use this string inside of your Regular Expressions,
now it is going to become a pattern. Now if it is a
pattern it is going to represent two strings, "Python2", and "Python3".
That's it. Now here we have to know how the square brackets are going
to work here. See based on square brackets
now this entire string is going to represent two strings.
That's why this is called a pattern.
Now we have to learn this type of rules to create a pattern.
Anyway, we will see them one by one, step by step, and guys
sometimes for your pattern
you can also use 'r', that is called 'raw string', but there
is some purpose for that.
We'll discuss that in your advanced level of your Regular
Expressions. Now guys, just to get an idea on how to create
a pattern or to understand the rules to create your pattern
I am going to take one of the operations from your Regular
Expressions that is called 'findall', because compared to all
this 'findall' is very easy.
So I'm going to take this operation just to give you
clarity on to create rules for your patterns. Then later
we will work with all operations.
See once you are good with pattern, then we can work with
any operation from your Regular Expressions.
[no audio]
Fine. Now see that.
[no audio]
'findall', this is one of the operations from your Regular Expression.
Now how to use that 'findall'? Simply you have to import your
're' module inside of your scripts on your command
line. Then 're.findall(pattern,text)'.
So this is your given text, in this text you are going to
search for something, search for some string. That we are going
to use, that we are going to find by using 'findall' operation
from your 're'.
So finally whatever you are going to get, that
I'm printing. That's it.
[no audio]
Right. See that. Just to give simple idea
I am going to write a simple
[no audio]
Python script. Let me save this.
Of course, you can practice this on your command line as well.
So 'practice_for_regex.py'. See first of all we are going
to work with Regular Expressions in your Python.
So we need to 'import re', and make sure that this is a default
module. You don't need to install your 're' module, by default it
is there with your Python.
Now, first of all let me take 'my_str', or let me say simply
'text'. You can take anything,
this is just a variable.
So, "This is a
[no audio]
python and it is easy to learn". That's it.
This is my string.
Now, my intention is, I want to search
a string called "is" in
a given text.
See that. What I am doing is, simply I'm going to create
'my_pat'. Pattern is nothing
but, what is your required string.
What is the required string you want to search in a given
text, that is a pattern.
So I want to search only "is".
Now, let me simply 'print(re.findall())', your pattern in
a given text.
So guys these are the variables. So directly
if you want to write your pattern here, and your text here
you can write it. I will show you that as well.
Now, let me run this and see the output. See in your entire text
in how many places "is" there?
See, guys be clear.
We are not going to work with word. Word means, before your
starting letter of a word
you should have some space, after ending letter of your word
you should have some space, then that is a word. But as of
now you're not going to work with a word, you are going to
work with a string. See
"is" is there with the combination of this word, but we are looking
just simply "is" string.
So here "is" is there, here "is" is there, here "is" is there, so
all those "is" you're getting. See what I am saying is, yeah
while working with your Regular Expressions, directly
you can write your pattern here and your text here. See
I am going to write this thing.
But instead of writing, just I stored in some variables, and
now I am working with variables.
There is no change. Right.
Anyway, this is not a good practice. First store your text into
some variable and your pattern into some
variable then use that variable.
[no audio]
Fine. Now how it is going to work?
Very simple. What is your pattern? "is".
So two letters string you're searching. What your Python will
do, from your given text it will take first two characters. Then
is this, are these two characters equal to these two characters?
No. That's why it will skip that part. Then it will take second
character and third character,
"h-i". Are they equal to "is"?
No. Then it is going to skip. Then third and fourth it will
take. Yes, "is" and "is". Now
it is going to match your going to get. So likewise
it is going to compare in your entire text, wherever there
is a match for "is" those strings you are going to get.
Be clear. Not a word, just simply you're going to match for a string.
Even word is also a string.
See, here "is" is a word but here "is" is not a word. That is a combination
with some other word. But we are not looking for a word.
We are looking for a string. That's it.
Now, can you tell me, very simple logic here,
[no audio]
that is, how many times "is" is there in a given text?
How many times "is" is there in a given text? See if you observe,
the operation result, 'findall' operation result is a list
no. We know the length we can find for a list by using 'len()'
operation so that you can conclude that
there are three "is" strings are there in a given text.
It's very, very important one.
[no audio]
That's fine.
[no audio]
So first let me write once again, 'print(re.findall())',
your pattern, given text.
[no audio]
Fine. Now my intention is, actually, the basic purpose of your Regular
Expression is at a time
if you want to search multiple strings, then we are going
to work with Regular Expressions.
Now I want to print
from my given text wherever "is" is there, I mean "is" string
and "it" string,
all those things I want to print.
See my intention, be clear,
I want to search for "is" and "it" in a given
text. So if "is" is there it has to print, if "it" is there it has
to print. Suppose if "is" is there multiple times, all times
it has to print. If "it" is there multiple times, all "its" it has to print.
But now you have to understand that
this, here you are having two strings,
but from this you need to make a pattern
such that that pattern has to represent these two strings,
so that by using that pattern you can search in a given string
for these two strings.
See, if you observe first character is common
no. That's why I can take simply "i". Then second character
it maybe "s" or "t". So whenever there is a possibility of this
one or that one then just simply write square brackets, inside
of that just write
"st" or "ts", no problem, both are same.
Right. Now see that. What I am doing is, instead of "is",
first character is "is", sorry "i", but second character
maybe "s" or maybe "t".
Now see the result - "is", "is", "it", "is".
Yes, you are getting. But you don't need to follow order here.
I mean first "s", second "t". No, not like that.
You can also write "ts" also, no problem. See that.
Now you are looking for a string which consists of
two characters first of all,
and that string, two character string
maybe "is" or maybe "it". Now you are going to search for two
strings with length of two, that means two characters in each
string. Those strings are "it" and "is" in a given text. Now if
they are in a given text then your 'findall' is going to find
them. Let's say suppose
I am trying to find,
let me comment this, I want to find like
'my_pat="x"'.
Do you have "x" in your given text?
No, that's why you're getting empty, '[]'.
[no audio]
Right. Do you have "a"? Yes. How many times "a" is there, that many
number of times you are getting. Here "a"
is there. Be clear. Very, very important. Here "a"
is there, here "a" is there, here "a" is there.
So wherever you have "a" all that "a" you are getting here.
So if you go with advanced operations like 'search', 'findall',
sorry, 'finditer',
then you are going to get the index position as well.
What is the index of first matching character, first
matching string, and second matching string.
We will see that in advanced level.
So for time being we have to understand how to create a pattern.
Now here I am going to show you the simple rules
to create your pattern but this is just first level rules,
basic rules.
So we will see advanced level rules in next video. Fine.
The very first thing is, suppose
I want to match "a" with "a", then directly you have to write
"a", there is no other option.
I mean, suppose here I have written "a", right?
So you are going to match "a" with "a", then you are getting.
Suppose I want to match with some number, if number is there
9 that to 9, if that 9 number is there in a given string, then, given
string means text guys.
In this 'text' variable if you have a 9, then Python
will find that. But as of now, you don't have any 9 number
in a given text, that's why it is not finding.
So simple rule is, if you want to match with any particular
character with particular character, then you
need to directly write your pattern as that character only.
Let's say somewhere
I'm having "@".
Now I want to search how many times "@"
is there in a given text.
See that. One time.
Let me write somewhere
once again this "@". Now
let me rerun your code and see the result. In two places you
have "@", now those two "@" you are getting.
[no audio]
Fine. Now if you want to match any character with particular
character then you have to write that character itself.
That is, anyway, we know that generally. Second rule, see just
now we have seen that if I write something like '[ts]' or '[st]',
what is the meaning for this? It is going to look for either
"t" or for "s". But same way if I write '[abc]', if I write '[abc]' now it
is going to look for either "a" or "b" or "c" in a given text.
Let me show you that so that you can understand.
Suppose I am writing as of now
'my_pat="@"'.
Now your 'findall' is going to match or look in a given text only for "@".
Now, what I am doing is, if I write something like "@a",
what is the meaning for this? You are going to look for
a string which consists of "@", and "a" immediately, side by side
"@", and "a". Do you have anywhere in a given text? No, nowhere
we have "@" and "a" immediately in a given text. Bow see
the result, '[]'. But I want to look either for "@",
or for "a", or for "s",
you can write any number of characters inside of square brackets.
Now the meaning is, assume that your Python is going to search
for "@", also for "a", and also for "s". Now see the result.
How many times "s" is there? That many times you're going to
get "s". How many times "a" is there? That many times you are
getting. How many times "@" is there? You are getting.
Now, first of all you're looking for only one character string
in a given string. That character maybe "@", maybe
"a", or maybe "s".
So all these three are there some number of times, you're
going to get all of them that many number of times.
[no audio]
Right. Fine. Now see here.
[no audio]
So sometimes I want to, just assume that I want to look for
either a or b or c or d or e or f.
[no audio]
So what is the meaning of, if you write any characters
inside of a square bracket means, first of all this entire
thing is one character.
But that character maybe "a" or maybe "b" or maybe "c", "d", likewise.
So I can write in this way if I want to look for either "a"
or "b" or "c" or "d" or "e" or "f", but all these are sequence characters,
right, "a" to "f".
So instead of writing in this way,
there is a shortcut for this. You can also write simply "a-f".
Now the meaning for this is you are going to look for one
character string but that character maybe "a" or maybe "b", or
up to "f".
That's it. Now assume that
something like this,
I want to search a string,
[no audio]
let's say '[abcd, either "a" or "b" or "c" or "d", or some 'hijkl',
or 'xyz]'. No spaces guys. Just assume that I have written
'[abcd hijkl xyz]'. Now
what is first of all the meaning for this? If you write anything
inside of a square bracket, it is going to look
for one character string, but that character maybe "a", or maybe
"b", something like that from this group.
But if you observe, first four characters are sequence, next
five characters are also in sequence, next three characters
are also sequence. In that case
you can also write shortcut as 'a-d',
now by default 'a-d' means '[abcd]'. Then '[h-l]'. The meaning
for this is [hijkl]. Then '[x-z]'. Now the meaning for this
is [xyz]'. Just for this, shortcut this is.
[no audio]
Right. Now see that. Let me take a given text.
So my intention is, let me write a, let me comment this code
as of now. Actually my intention is, just I want to look for
either "a" or "b" or "c",
or "d", let me take it. So 'my_pat', I want to look only for
either A or B or C or D whenever if you want to look for
either "a" or "b" or "c" or "d". Then you have to write your characters
inside of square brackets. Now, let me 'print()',
your operation is from 're.findall()', from your pattern,
your given text. Now see the result.
[abcd]. "c" is not there nowhere, and "b" also not there, but "a"
and "d" is there, that's why you're getting that - 'a', 'a', 'd', 'a', 'a'.
But the same thing I can also write '[a-d]'.
[no audio]
See the result. First two "a", one "d", two "a'. Now see the result,
I am going to rerun by changing my pattern. Same thing you're
getting. So this exactly equals to '[abcd]'.
See sometimes you're going to look for any character, suppose '[a-z]',
no need to write 'abcdefg' up to 'z'.
Simply 'a-z' you can write. '[a-z]'. That's it.
[no audio]
So this is one of the rules.
Then, so that's what I am giving here.
So this already we discussed. Suppose if you have 'abcd',
some 'hijk', and 'xyz', you can also write shortcut in this way.
Now, let me take '\w'. Matches any single character, or digit,
or underscore.
[no audi]
See that. See guys if I write simply pattern as 'a', "a" is going
to match with only "a". Your 'findall' is going to look for this
"a" in a given text for "a" only. But if you write instead
of "a" if I write some special symbol called '\w', now the
meaning for this is any alphabet from a to z, or
capital A to Z, then number 0 to 9 or maybe underscore also.
[no audio]
See suppose if we have a text called 'abc', and if I write a
pattern "a", now if I look into this "a" is going to get only
one time, but if I write in place of "a", '\w', "a" is also a '\w\,
or "b" also comes under '\w', "c" also comes under '\w'. Now you're
going to get three characters because '\w' means any any alphabet,
and number, and one underscore as well.
Let me show you this on your script.
See my intention is,
let me take, now,
first of all let me comment this code.
Guys be clear,
[no audio]
I am going to take 'my_pat' as 're', sorry.
I'm not taking raw string as of now, but we have to take
it. I will tell you when you have to take 'r'. Fine.
Now, what I am doing simply I am writing '\w'.
Now see the result. If I do 'print(re.findall())', your
pattern, in a given text, see the result.
You're going to get all matching things except space.
Are we getting space matching? See after this "is", between "This"
and "is", we have a space. Are we getting a space anywhere here?
No, because space does not come under '\w',
that's why you're not getting matching for that.
And "@" does not comes under the your '\w', that's why this
is not a match for your '\w'. That's why you're not getting
that. Let me write somewhere "-", and let me write somewhere
"_". Now see the result.
You're getting "_". Between "o", and "l", there is
an "_", but between "o" and "_" you have a
space, that space is not a match.
You're not getting that. And "-" does not come under this
'\w', but "_" comes under '\w', that's why you are getting here.
[no audio]
Now I am looking for two letter string or two character
string. If I write '\w\w', the meaning for that is just
assumption. "a" means, sorry, '\w' means 'a-z' small letters,
and 'A-Z' capital letters, and numbers 0 to 9, and underscore.
Now if I write two '\w' the meaning for that is, it may be
'aa', 'ab', 'ac', likewise 'A-Z' and 'ba', 'bb', 'bc',
likewise, or maybe 'a0', 'a1', 'a2' up to 'a9', or 'a_',
right, 'b_', 'c_', likewise you are going to get
number of combinations. Among all those combinations
whatever matching is there in a given string you're going to get them.
See if I write '\w\w' the meaning for that is, you're going
to look for a string which consists of two characters.
Now if I run this, see "th" is also one match for this. First,
let me run this and see the output so that you can understand.
See that. "Th", two letter string,
yes, match. That's why we are getting that. "is", yes you are getting,
then "is" you're, in between you have a space, right, space does not
come under your '\w', that's why you are not getting that. "is",
yes you are getting. See "a" is only one single character. Before
"a" and after "a" you have a space.
So combination with "a", you don't have any valid character
which comes under '\w'.
In case if space comes under '\w', you're going to get "a "
also, but space not comes under '\w' matching,
that's why for "a" you don't have any combination to get two
character strings.
That's why this is not going to match for your pattern.
[no audio]
Right. And one more thing, see that last "le" suppose, two
character string, "ar", two character string, but for "n"
you don't have again any matching to get a two character string.
After "n" suppose if we have any character,
let's say "a", now if I run this. See the last thing, you're
getting "ar". After "ar" now I already here you "a" letter,
"a" character. Now if I rerun you're going to get "na" also.
Not only "a", you can take "_" also, because "_"
also comes under '\w',
you are going to get matched by using these two characters
combination. Now, let me write three, three character
matching. Guys,
now the meaning for this is, you're going to look for a string
which consists of three characters, but what are those three
characters? We know '\w' means any character 'a-z' small or
capital, 0 to 9, and underscore. Only from these three groups
it is going to look for three character string.
Now, let me run this and see the result. "thi" three-letter character,
yes, you are getting. But for "s", after that you have a space,
so space does not come under '\w', that's why there is no possibility
for this "s" to make three letter character. Then it is going
to skip. Then for "is" you have only two characters, but you
don't have any three characters string, because if you want
to get a three character string here we have a space, but
space does not come under '\w', that's why you're not getting
a part for this. Same way for "a" you don't have any three character
string, but "python", "pyt",
three characters, valid characters, "hon", three characters. That's why
you are getting that. "and", three characters. For "it"
you don't have any valid three characters,
that's why you're not getting. "eas", yes, "e-a-s", three characters,
but for "y" we don't have any three character string combination,
that's why this is not a part. Likewise you are getting. I
hope you're good with this.
[no audio]
Now, let me go with the capital '\W' - Matches
any character not part of '\w'.
See whatever you have in this '\w',
other than that all are going to come under '\W'. See that.
Now, let me comment this.
[no audio]
First let me take 'my_pat' as small,
[no audio]
'\w' only, and see the result first. 'print(re.findall())'. Guys
you can get clarity by doing practice.
Please do same thing
once, I mean whatever we've done here same thing you just practice
once, and try to understand how you're going to get your output.
[no audio]
Now, let me run this and see the output. But if I make it
as a capital '\W', see "T" and "h", "i", "s", whatever you are getting here
no, these are not the part of '\W'.
They are part of '\w'.
That's why if I write '\W', see what you are going to
get. You're going to get spaces, "@", and "-", because
these are not the part of your '\w'.
It is going to look for a character which is not there in
this group, '\w' group.
Space is the match for '\W', "@" is the match
for '\W'.
See how many spaces are there, all those spaces you are
going to get here.
So that is the use of '\W'.
Simply the members which are there in this, other
than that remaining all come under '\W'.
Very, very important.
Then '\d' - Matches a decimal digit 0-9,
any number. See any way by default '\w'
means it also includes your numbers but along with the numbers
'\w' includes alphabets and underscore. But I want to match
with only numbers.
Let's say I have somewhere, let me write "PYTHON3".
[no audio]
"python3",
and let's say, somewhere just for, here
I am writing "python2", and somewhere
I'm writing some number let's say 4.
Okay. Now what I am doing is, I am going to take a pattern as some
'\d'. You know what is meant by '\d'. Any number 0 to 9.
See I'm going to take pattern as '\d', and let me print it.
're.findall()'. Sorry.
'findall()', your pattern, in a given text.
So wherever number is there, because you're looking for only
one number, one string,
sorry, a character which consists of only one digit, string
which consists of only one character that is a digit. Now
see the result. [2, 4, 3]. Yes,
we have somewhere [2, 4, 3]. You are getting that. Now my intention
is, "python" with some number. So before '\d'
I should have a word called "python", after that any number
maybe 2 or 3. Now,
if I run this you are going to get "python", after that number
is there, no. Now this is the match for this pattern. At
the same time "python3" is also match for '\d'. Suppose
if I write only "python2", you're going to get only "python2"
because directly you're mentioning match "2" with "2", "python2"
with "python2". But I want to match with "python\d" means,
the meaning for this, it is going to represent "python0",
"python1", "python2", "python3", up to "python9". But among
all those whatever the strings are there in a given text
those things, those strings only it is going to match.
That's it.
[no audio]
Right. Now let's say instead of this pattern
I want to look for
a string which consists of two digits. Do we have anything
in this? No. Sequentially,
I mean immediately two digits nowhere we are having. That's
why you're not getting that. Let's say I want to write here
45, something. Now you are getting that because two digits are
there. You're looking for '\d\d' means a string
which consists of two digits.
Yes you are getting.
Now, so in this basic rules, let me go
with the last one, '.'.
Very, very important one, at the same time confusion one. Very
useful in your real-time, '.'. '.' matches any single character,
except new line character.
[no audio]
See if you take '\W', other than this members all comes
under '\W'.
But if you go with the '.', all valid characters which are
there in your keyboard or which are there in your computer
language, all are going to match with your '.' except new
line. See that.
[no audio]
Let me comment this things first.
[no audio]
Guys these are very basic rules to create your patterns.
We have advanced levels as well,
we will see them in our next video.
So now my intention is, I am taking a pattern as
[no audio]
'.' means, match with any character. Now see that what you are going to get.
[no audio]
Your pattern, 'text'. All we are going to get. See that. We're
going to get each and every character which is there in a given
text. Anyway, we don't have any '\n', I mean new line in our
given text, except that that is going to match all things.
Now if you observe "T", "h", "i", "s", after that space is there in a given
text. Yes, that space is also matching for '.'. '.' means,
a '.' means anything simply other than '\n'.
Now I want to match
any two characters, then write two '.'. Now see the result.
I want to match for three '.',
I mean any three characters, any three symbols which are there
in computer language.
See that.
"33" you're going to get. Now
my intention is, I want to match '.' with '.' only.
[no audio]
Generally '.' means, in your pattern '.' means for any matching
except new line. But I want to match '.' with '.'.
Let's say in a given text somewhere I have '.', "python 2."
Now if I run this what you are getting?
[no audio]
See now you're also going to get somewhere "." also.
You're looking for
[no audio]
three digit string, yes.
Sorry, three character string.
Yes, you are getting, 'n2.'. But along with the '.' you're also
getting some other strings
no, but I don't want to get them.
I want to get exactly '.' with '.' only.
I want to match my '.' with '.'.
See if you remember, if you want to match "a" with "a", let's say
this is your text "abc".
This is your text, and you want to write a pattern. How you
can write a pattern if you want to match "a" with "a"?
"a" only you're writing.
Now in my text somewhere I have '.', but I want to match
"." with ".". Simply if I write ".", "." is going to match with
any character.
But I want to match this "." with this "." only. At that time
you have to escape your special purpose of your "." by placing
'\' symbol.
Let me run this.
[no audio]
'\.', now see the result.
[no audio]
Let me write some other ways, in other places ".".
See that. That's it.
[no audio]
But if I remove this '\', now you're going to get all characters
which are going to match for your ".". By simply "." means
anything. That's it. Except new line.
So it's very, very important guys.
If you want to match "." with ".", you have to use in your
pattern '\.'.
Very, very useful example
I am going to give in your real time.
Let's say you have a text in this way.
"This is my ip of a db server.
Guys just assume that some, '255.',
let's say
[no audio]
'100.'
[no audio]
'102.' Of course
[no audio]
as of now I am taking in my IP in all places, in all places three digits.
But see, you may have sometimes here only two digits or one digit, right.
But as of now based on our rules, if I go with this example
you can understand. Later
we will see in our advanced level
if you have only one digit here how to match, everything
we will see. Now I am going to tell you one important thing
from your real time.
See you are going to run some script, or some information
is there somewhere in your text file, and you are going to
read that, but you don't know where your IP is there.
Assume that you're getting some output somewhere. In that
output you have IP, but you don't know where exactly your IP is there,
[no audio]
where exactly your IP is there but I want to fetch only my
IP. How you can write a pattern?
See, I can write a pattern, any variable, just take any variable.
I am taking simply now 'pat' or 'my_pat', or 'pat', or
'x=', also you can take. See as of now
I am assuming that in my IP in all places three digit number
is there. That's why '\d\d\d' means,
[no audio]
you are looking for
three digit string in a given text.
But first let me run this so that you can understand.
[no audio]
What happened? Yeah.
Sorry, I have written at the end one more extra '\'. Fine. Now see that.
'findall()', your pattern, your text.
See the output.
[no audio]
Okay, let me write three digits. Now see the output. '255',
'100', '102', '103', you're getting separately.
But this entire thing is one IP.
I want to get as only one value, not like this. Then what I have to do?
I want to write a pattern such that it is going to match with this thing.
I don't know exact number what is there but I know in each and every
position there are three digits then three '\d\d\d', then '.\d\d\d
.\d\d\d.\d\d\d'
Now see the output.
It's working perfectly, right?
[no audio]
It's working perfectly. Now see the magic.
Now let's say
[no audio]
here instead of '\d', what I am doing is '\w' I am writing.
[no audio]
Just assume that. Because under '\w'
you're also going to get digits, right?
[no audio]
Now see the result what you are getting.
You are getting.
Now I am writing "this", something, in this
way I'm writing. Now see the result what you are getting.
[no audio]
Nowhere you have "." here
no, then why it is matching with your given pattern? Because
"." means anything, but I want to match "." with "." only.
I want to match "." with "." only.
Otherwise, let me take in terms of numbers only so that you will get idea.
Let's say 1, 2, 3.
[no audio]
One extra number.
Let me write some numbers,
[no audio]
some number.
[no audio]
This is not IP, right, but I'm writing some number.
Now let me write instead of this thing.
Guys be clear why I am giving this means, in your real time
you're going to get sometimes confusion, for that
I am giving some clarity here to use your '\d'.
[no audio]
Now if I run this, see the output. This is IP, but this is
not a IP right?
Our intention is, three digits, then '.', three digits, then
'.', three digits, then '.', three digits. In this way if
I have any string in a given text that only I want to fetch.
Yes, I'm fetching that. Along with that why I'm getting this?
See now you are not matching "," with ".".
If you want to match this "." with this ".", you don't
write '.', you have to write '\.'.
Simply if I write a '.', '.' means any digit, any character.
That's why see '\d\d\d', this is going to match for
first three digits.
Then '.' means anything
no. Now this is also going to match. Again three digits.
Yes, three digits. Then "." means anything. '5' is also going
to match for ".". But our intention is not for this type
of numbers. Some three digits, then ".", some three digits, then
".", then some three digits, ".", likewise.
That's why if you want to match "." with ".",
you have to use '\.'.
That's it. Now see the result.
[no audio]
So that's why there is a small difference between '.' and
'\.'. You need to understand that clearly.
Simple thing, I want to match "." with "." only, then you
have to write '\.'. That's it.
Okay. Okay guys, don't get confused.
We are going to practice with number of examples so that
you will get good idea about your Regular Expressions.
So as of now, these are the simple
rules to create your pattern. In next video
we'll discuss about advanced level rules to create your pattern.
Okay, thank you for watching this video.
[no audio]