Skip to playerSkip to main content
  • 19 hours ago
Make Python regular expressions easier to read and use by learning the character classes that help find specific patterns in text. This practical coding lesson demonstrates how `re.findall()` works with `\d` and `\D` for digits and non-digits, `\w` and `\W` for word characters and non-word characters, and `\s` and `\S` for whitespace and non-whitespace. Examples use names, phone numbers, email text, and punctuation to show what each pattern matches.

The tutorial also explains word boundaries with `\b` and `\B`, and briefly compares `\A` and `\Z` with familiar start- and end-of-string anchors. A useful Python regex guide for beginners learning pattern matching, text searching, and how to build more precise regular expressions. Find clear examples and explanations for Python regular expression character classes, regex shortcuts, whitespace matching, digit matching, and word-boundary searches—ideal for coding practice, study, or a quick reference while working with strings.

Category

📚
Learning
Transcript
00:00Hello, everybody, and welcome back to the Regular Expression Python series.
00:03In this lesson, we're going to be taking a look at regex character classes.
00:07Now, these character classes are just going to make it a little bit easier
00:10to identify specific patterns within our string or text
00:14that we're going to be working with.
00:15At the very bottom, you should actually recognize some of these patterns,
00:17like put these brackets A through Z or A through Z.
00:21This is actually very similar to using something like the backslash capital D.
00:26And so there are ways you can shorten and make this a little bit easier,
00:29as well as look for certain unique things.
00:31Now, one thing to mention really quickly is there is one on here,
00:35and I just could not find a good visual for this,
00:37but there is another one on here called backslash W, which stands for boundary.
00:41We'll also look at that in this lesson,
00:43even though I couldn't find anywhere that talked about it.
00:46But if we look really quickly, we have backslash D, which matches digits.
00:49Usually the capital letter is the opposite of that.
00:52So then we have capital D, which matches any non-digit.
00:56Backslash W matches any alphanumeric character.
00:59And then the backslash W or capital W is the opposite of that.
01:03Backslash S stands for matching white space.
01:05And then capital S is non-white space.
01:08So we're going to be taking a look at all these and how we can use them.
01:11Let's scroll right down here.
01:13And let's start off by creating our quote or our string.
01:16We'll call this quote in this lesson.
01:18And we can say, my name is Neo.
01:22Neo, my phone, my phone number is random numbers.
01:29There we go.
01:30And that's one too many in here.
01:32And then we'll say, my email is the matrixman at gmail.com.
01:41Neo uses Gmail, I'm pretty sure.
01:43So let's go ahead and create this.
01:45And let's see how we can use these character classes.
01:48So what we're going to do is re.findAll.
01:52And again, we're using our quote.
01:55So we're just going to specify that.
01:56Now let's start off with probably the easiest or the simplest one, which is backslash D.
02:01This stands for digits.
02:02So we're looking for the digits, which are all right here.
02:06Let's go ahead and run this.
02:07And of course, I need to actually import regular expression.
02:11So let me put this up here.
02:13We'll import regular expression.
02:16Run our quote again.
02:18There we go.
02:19So now we have all of our numbers all broken out.
02:22Now what if I wanted to look for more digits?
02:24I could use these squiggly brackets.
02:26And I could say I want to look for three digits.
02:28Now let's run this.
02:30Now we're grouping the digits based off of three.
02:33We're looking for three digits in a row.
02:35So now we have five, three, four right here, three, four, two right here.
02:40But then this has one, two, three, four.
02:42There's four digits here, but it only takes the first three.
02:45This would be extremely similar if we did like this.
02:51And we said zero dash nine.
02:53And if we run this, we get this exact same output.
02:56But this one's just a little bit longer.
02:58And we can go ahead and specify other things in this as well.
03:02I personally have found that when I'm working with digits,
03:04I'm looking for all the numbers.
03:06So I tend to find myself using this one quite a bit more than using the brackets with zero dash
03:12nine in it.
03:13But if we took this very similarly,
03:16and instead of the lowercase d, we did a capital D.
03:21If we run this, it's going to take everything that is not a number.
03:25So all of the non-numeric numbers, even the periods and the spaces and all that stuff and the dashes
03:32right here in between the numbers.
03:33So it's taking literally every single thing except for the numbers.
03:37It's the opposite of the lowercase d.
03:40So let's copy this.
03:41And we're just going to go right down here.
03:45And we're going to take a look at backslash w.
03:48And again, this is our alphanumeric character.
03:51So if we go down here, say re.findAll.
03:54And we're going to say comma quote.
03:57And when we do a backslash w, we're going to almost get everything except we're really taking out all the
04:06fillers.
04:07All of the periods, all of the white spaces, all of the dashes, all of these things are taken out.
04:14Now, what if we change this to a capital W?
04:17We'll get the exact opposite.
04:19So now we're taking all of those filler things.
04:21We're taking all the spaces, all the dashes, the periods, the at signs.
04:25We're taking all of those things, which is the exact opposite of what we get with the lowercase w.
04:31Now let's go ahead and copy this quote.
04:33We'll come down once again.
04:35Now we're going to be taking a look at the backslash s.
04:40Now lowercase s is going to match any white space characters like spaces, tabs, new lines, or things like that.
04:46So if I say re.findAll, comma quote,
04:50we're just going to do backslash s.
04:52Keep it really, really simple.
04:54So backslash s.
04:56Now it's going to take the white spaces, which is spaces, tabs, or new lines.
05:00It's not going to take things like periods.
05:03It's not going to take things like dashes or at signs.
05:07It's just looking mostly at white space.
05:10Now if we do a capital S, just like this,
05:15we're looking at everything but the white space.
05:17Now it's every single character except things like spaces or tabs or enter,
05:24which is the backslash n or new line.
05:26So now it's the exact opposite.
05:28That's all the backslash lowercase s and capital S.
05:32That's all that is.
05:33Now the next one that we're going to take a look at is going to be,
05:36was going to be backslash b, which is boundary.
05:39And I think that one's really interesting.
05:40But before I do that, I am going to show you two that I almost never, ever use, almost ever.
05:46But I'm going to show it to you anyways, just so that, you know,
05:49I feel like I've showed you everything.
05:51I'm not withholding things from you.
05:53And what we're going to do is re.findAll and actually put right here.
05:58We'll look at the backslash a and backslash z.
06:03So we're doing re.findAll.
06:06We'll do comma quote.
06:08And what we're going to do, we're going to use our quotes and say backslash a.
06:14Backslash a and z are kind of like a caret and a dollar sign in the meta characters.
06:19You're looking at the beginning and the end of a string.
06:21Now, if I just run this, I'm going to get nothing.
06:25But what if I looked for a bracket and I did like a to z or a dash z, a
06:34dash z.
06:35And let's run this.
06:36Now I was able to find the very first character because we were looking at capital M.
06:42So we found that.
06:43But if we did z, now that's not going to work.
06:47Much like the dollar sign, this needs to go at the end now.
06:51If we want to look at the end.
06:53So let's run this.
06:54Now we found the M at the very end.
06:56These two I almost never use.
06:58Unlike when you're using a caret and a dollar sign, I feel like the A and the Z are just
07:02pretty much, they're redundant.
07:05They do the exact same thing as something else in the exact same way, except you use backslash z instead
07:10of just doing something like a dollar sign, which gives you the same output.
07:15And the dollar sign, I think, is just easier to spot, easier to visualize.
07:18And whenever I use it in my actual job, that's how everyone else did it as well.
07:23No one ever used those backslash z's or a's.
07:25So again, just wanted to show it to you, but not something that I really actually use, if I'm being
07:30honest.
07:30Now let's go on to B, which stands for boundary.
07:34Now for this one, we are going to actually use a different quote.
07:37We'll keep it on the similar theme.
07:39We'll say, I love the matrix.
07:44Is matrix love a word?
07:47Now this makes absolutely no sense, but it'll make sense in just a second.
07:52So let's go ahead and bring this down.
07:54And we're going to get rid of all of this.
07:56And if we wanted to search for just the matrix, we can do that.
08:01Let's go ahead and search.
08:02And we're able to find both.
08:04But we can use these lowercase b's as boundary points.
08:08So we're going to say backslash b and then backslash b.
08:13So we're looking for a boundary on the left-hand side and a boundary on the right-hand side.
08:18Kind of like white space.
08:20And what we can do is look for that.
08:22We're not getting an output.
08:23Let's actually put it on right here.
08:26There we go.
08:27So this represents raw string.
08:31So now we're looking at a raw string literal.
08:33That's what this is called.
08:34I'm not going to go into what all that means.
08:36But basically it'll change this from just a regular string into a raw string literal,
08:41which sometimes definitely can matter in regular expression.
08:44And we'll look at that in the next lesson.
08:46But just wanted to show that to you.
08:48So putting that quick fix.
08:50But we're looking at boundaries.
08:52So we have a boundary on the left-hand side.
08:54We have a boundary on the right-hand side.
08:56It's only going to pick up this matrix.
08:58Because this has L-O-V-E on the right-hand side.
09:03It has a boundary on the left.
09:04It does not have a boundary on the right.
09:06Now if I get rid of this second boundary right here and I run this, we'll get both because
09:12this matrix has a boundary on the left and this matrix also has a boundary on the left.
09:17Now what if I wanted to use the capital B?
09:21Let's do a capital B.
09:23Let's run this.
09:24Neither of these work.
09:25And that's because we're looking for a non-boundary.
09:29And so we're looking for matrix, but on the left-hand side, we have no boundary.
09:34And that could either be a period or it could be another letter.
09:37So if we just got rid of this right here, we're looking for atrix, but with a non-boundary,
09:43which should be that M.
09:44We should be able to find both of those.
09:46So those are our character classes and the meta characters and the character classes separately
09:52when you're learning them in two different lessons, they may not fully 100% make sense.
09:57And I try to do my best to visually see it.
09:59But where it really shines is when you're looking for actual patterns.
10:02So in the next lesson, we'll be looking at use cases.
10:06So real use cases where you can use all these things in combination to specify very specific
10:12patterns.
10:12And that's really the power of regular expression.
10:16So again, this is the end of the character classes lesson.
10:18Thank you so much for watching.
10:20If you liked this video, be sure to like and subscribe below.
10:23And I'll see you in the next lesson.

Recommended