#100DaysOfCode in Python Transcripts
Chapter: Days 28-30: Regular Expressions
Lecture: When not to use regexes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's dive straight into a notebook
0:02
I've prepared for this class.
0:04
It's sometimes said that I have a problem,
0:07
I use a regular expression knife too,
0:09
and in a sense that's true,
0:11
that they're intimidating when you start.
0:13
But there are only a few rules so
0:17
get some practice and you will see that
0:18
they're not that difficult.
0:20
Let's import the re module.
0:22
First of all, there are cases that you don't
0:24
want to use a regex.
0:26
The standard string methods are pretty powerful
0:29
and cover some of your needs already.
0:31
For example, I have a text.
0:33
'Awesome, I'm doing the #100DaysOfCode challenge'
0:36
And we want to see if that string starts with 'Awesome'
0:39
so no regular expression needed for that
0:42
you can just do text starts with
0:45
Awesome and True.
0:49
Or does it end with
0:53
'challenge'?
0:55
It does.
0:57
Or does the case insensitive version
1:01
of text has '100DaysOfCode' in it?
1:04
Now for that you'd first want to lowercase the string
1:08
and then you want to see if
1:11
100DaysOfCode
1:14
is in that string.
1:19
And it is.
1:21
And what about replacing?
1:23
So I am bold and I'm taking 200 days of code.
1:28
I don't recommend that by the way.
1:30
Well you can just do a text, replace
1:36
100 text strings
1:38
by 200
1:41
and awesome, I'm doing the 200 days of code.
1:45
So for these kind of string operations,
1:48
you don't really need a regex.
1:50
So look at the string operations that are available
1:53
in Python and use those instead.