00:00Topic for today's video is 'Regular Expressions'
00:02"How can we use Regular Expressions in Python?"
00:05And "What are Regular Expressions?"
You might not know that
00:08No problems, because in this video I am going to
make you understand everything practically
00:13Let's move to the computer screen
And let's get started
00:25OK, so let's explore 'Regular Expression'
00:29As you can see I've made a Repl
and I've opened it
00:33And before explaining 'Regular Expression',
Let me first tell you why you have to use it
00:38Firstly you don't have to use
'Regular Expression' for basic matching
00:42Because for basic matching you can use these keywords
or you can also check it by using 'For Loop'
00:50Yes I know you can check complex pattern
by using For Loop & making more efforts
00:55But 'Regular Expression' were made so that
you don't have to make more efforts
01:01And you get everything built-in
01:03To use 'Regular Expression' you have to import
're' package, It's a built-in package
01:09So this was the first step which I
wanted to tell you
01:12You can import 're' module
01:15Then there are 2 things, The first one is 'Pattern'
Suppose this is my patters
01:24As you all know, you can write Python
string in triple-single quotes(''' ''')
01:29Suppose I open Wikipedia
01:38Yes! OK! I'll write any thing
"Wikipedia"
01:45And I'll paste it here
so this is my text
01:48I've written my text here
01:49And it contains a lot of text
01:52This is my pattern & this is my text
01:54And I have to search this text through pattern
01:58Means I want to search where
this pattern is, in my text
02:02So 're' package, which means I can do that
with the help of 'Regular Expression'
02:05Now how will I do that?
02:07There's a function "re.search()"
02:10So firstly let me show you the pattern
02:13Suppose I want to search "was" from this text
02:18So after doing this if I print(match)
02:21So it won't tell me, where "was"
was present in this text
02:24But it will definitely tell me whether
the "was" exist in the text or not.
02:29Now I'll run it, so look, "Match "Was" found"
02:38So this is how I can tell if "Was"
exist in this text or not
02:41Now you'll think what's going on &
What's the point of doing all this?
02:44"I could have done it with the
help of these keywords"
02:49But how about if I make the pattern
a little bit complex?
02:52And here comes the main topic
'Regular Expression'
02:57Now let me define Meta characters
03:00Meta Characters are used to make patterns
03:03For eg. If I ask you to search or
Let's say we have "Cyclone" written here.
03:09Here we have "Cyclone"
03:12And here we have "Dyclone"
03:15Suppose it's written here
03:17Now if I ask you to tell me where we have
"yclone" starting with capital letters
03:24Which means "Dyclone" with Capital D
03:27"Eyclone" with Capital E and
"Zyclone" with Capital Z
03:31You have to search only these
03:33Now you'll be confused and you'll say
I'll create my custom logic
03:37But you'll ask me for time to think
03:40So to skip that time I'm here to tell
you about 'Regular Expression'
03:47Now I can specify here that the first letter of the word should be
a Capital letter and that word should also contain "yclone"
03:59pattern = "capitalyclone"
04:02I can specify it in this way
04:05I'll add "r" here and this string
will now be a raw string
04:09Let me tell you what is raw string
04:11Suppose if I write "back-slash(n)" then it will print a raw
string and won't parse escape sequence character
04:18I've added a square-bracket here
04:20I specified that any character will work for
me between Capital A and Capital Z
04:28But that word must also contain "yclone"
04:31What I've done here is, I've
made a character class
04:38I know many of you will be scared and say we're not getting
what you're saying, but pay attention on my words
04:41Any character between Capital A & Capital Z
04:49Must appear at-least once
04:51I need a word which contain any character between
Capital A and Capital Z along with "yclone"
04:57So look, (+) means "One or more occurrences"
04:59So If I add a (+) here and run it
05:04So "Cyclone" has been matched
05:07But "dyclone" didn't match, and
I'll have to see why.
05:11It didn't match because "re.search"
stops at the first match
05:18You will say that I just do not want to
know whether it's matching or not
05:22I'm not interested in first occurrence
05:26I'm interested in all the occurrences from the text
05:31So for that I'll write "match = re.finditer"
05:37And I'll write the pattern & text in the same format
05:40And now I'll write "for match in..."
Let me change it to "matches"
05:46And here I'll write "print(match)"
05:51Now if I run it so it'll throw all the matches
05:58As you can see it threw 2 matches
"Cyclone" & "Dyclone"
06:01And it also displayed the span of those occurrences
06:08Now I can write "for match in matches"
"print(match.span())" By this I'll get it's span
06:15Now if I show you it's type...
06:20So look, it's type will be printed here
And it's type is a Tuple
06:25Do you get the point?
06:28It means that I can do something like this
06:32I'll write here "print(match(*match. span( )))"
06:41If I do slicing for the match in this way
06:46Such that if I write here "match.span()[0]"
And here if I write "match.span()[1]
06:56Then I'll get those "Cyclone" & "Dyclone"
07:01Now it is saying "No such group"
Let me check what's the problem here
07:06I wrote "match.span()"
07:11I've to write "text" instead of "match"
07:13So I asked it to print "match.span()[0]"
& "match.span()[1]"
07:19Which means 0 & 7 of the text
07:21Now it is saying "string indices must be integers"
07:25I think they are not the integers
07:27So if I run "print(match.span()[0]"...
07:31It's saying "It's not an integer"
So let me see what's wrong
07:36If I only print "match.span()[0]"
07:40Then what am I getting?
I'm getting "0"
07:43Oh OK! I have to add colon(:)
instead of comma(,)
07:47I made a little mistake, but now see
I'll get "Cyclone" & "Dyclone" as output
07:52So this is how you can do matching
of 'Regular Expressions'
07:59Now again, 'Regular Expressions' is quite a big topic
You'll get to see a lot of things
08:02And it'll take a lot of time for you
to learn 'Regular Expressions'
08:05I've given a link here , so that you'll get to
know about all the meta characters
08:13And that link contains a lot of things
which you need to learn about
08:16Now if I talk about backslash(\)
08:18It marks the next character as a
special character or a literal
08:23For example, n matches the character n,
whereas (\)n matches a newline character.
08:28The sequence \\ matches \
08:30Which means it ends the specialness of (\) character
08:35So there are a lot of things which can be done
08:37So you have to understand the pattern
08:41And if you learn to write a good pattern then you'll
get all the things from the text that you want
08:46Or if you want to find if that
exist in the text or not
08:50Then you can do that too
08:52It's quite easy if you learn to write pattern
08:57Now I want to tell you about a website,
which is quite important
09:00First of all the python docs are quite important
09:03This is for those who wants to be a
master of 'Regular Expressions'
09:08It'll be like a book if you start
reading it from here
09:14It's a quite detailed documentation
09:15But yes, once you completed 100 Days
Of Code
09:18And you wants to be a master of 'Regular Expressions' then
you can become one from this documentation
09:23Another website which I want to talk about is....
09:27I forgot it's name , but look here it is
"regexr.com"
09:30It'll definitely appear on Google I knew that
09:31Look, from A-Z, then it becomes
group from bracket
09:34Let me show you, if I add this bracket
09:37And if I want to print one from them that
could either be "H" , "W" or "R"
09:45What does "\w" means? If you hover on it
then you'll know "Matches any word character"
09:50Any word character and more
than one word character
09:53Now look, I've written "/[HWR] +\w "
it means that...
09:59Any character from "H" "W" or "R"
10:02And then "\w" then there "+" which defines that it'll
take any character with multiple number of occurrences
10:11Which means "It would start with "H" "W" or "R"
and then any number of characters
10:17Here's a cheat-sheet and if you click on it
10:20Then you can learn a lot of things, let me tell you
"." means any character except new line
10:25So if I add "." after HWR, then....
10:28Then it'll match 2 characters, any one
from [HWR] and then any character
10:33So it matched "Re" "He" "Ro", Now if I add
double-dots(..) here then it'll match three characters
10:40If I make a group here defining "there must be two characters
after [HWR] and after that it should contain 'E' "
10:47So look, it matched this "Reference"
10:50I defined there must be any character from [HWR]
and two characters and then it should contain "E"
10:54Now I want "E" along with "R"
so it got match with this
11:03So this is how you can make patterns
11:05And in Python you can either use 'Match'
11:09If you use 'Match' so it return "None"
if it's unable to find
11:15Suppose if I try to find this,
then it'll show nothing
11:18Because the matches are empty
11:21But if I show you then this will return "None"
because this pattern didn't match
11:28This is how you can start your journey of 'Regular Expressions'.
I know it's a little bit complicated if you're a beginner
11:36When I started to learn 'Regular Expressions' ,
I also used to think I don't need this
11:40But as you progress in programming
you'll see it in JavaScript too
11:44And you'll see it almost in every programming language
11:48And it'll only make things easier not difficult
11:51So you must learn it and
let me link this website here
11:57So that you can explore it
12:02Let me add this here
12:04So don't forget to visit this website and it's cheat-sheet.
It contains a lot of things which you'll understand
12:11And the best part is that, it explains
'Regular Expressions' to you briefly
12:17When & why is it matching and and not
all the stuff like that
12:21So you play around with that by clicking here
12:26I hope you understood 'Regular Expressions'
12:28And you won't understand this briefly
until you practice it
12:33That's it for this video guys
12:35And make sure to access this play-list
if you haven't yet
12:39Thank you so much guys for
watching this video :)
12:41And I'll see you next time