Let's finally understand everything about regex. Regex is short for Regular Expression.
Goal of the Course
The purpose of this course is to help you understand regex. I'll be simplifying regular expressions in a couple of videos. I'll be using many examples to explain and show you different regular expressions you can write for different needs.
Regular Expressions can be interesting, powerful, but also frustrating to create or get correctly. In my programming journey, I've struggled with understanding regular expressions. And when I think I finally understand it, I see a problem where I need to use it, and I get confused on how to use it. And I know it's not just me.
So my goal for this course, is that you finally get a hang of it. You're able to read and understand regular expressions comfortably and also able to write complex regular expressions.
Can you read this?
Maybe you can't for now, but at the end of this course, we'll revisit this, and see if understand it.
Why do we need Regular Expressions?
Regular Expressions are used for creating patterns which match different parts of a string. Let's take this string for example:
“Hello, I have a bag worth $50, a shoe worth $100 and a hat worth $90”
Let's say we want to search this string for the word “bag”. It's easy right? We just need to search for a “b” followed by “a” followed by “g”. If things were always this straightforward, we wouldn't need regular expressions. But things are not always this literal.
For example, what if we wanted to search this string for a dollar sign, followed by a number.
You cannot simply search for a “dollar sign” followed by “5” followed by “0”, because even though that matches the “$50”, it does not match the “$100”. And it also doesn't match the “$90”.
So for you to be able to accurately search for the part of this long string which starts with a dollar sign, and is followed with a number, be it 5, 10, 100, 1000, you need a regular expression.
Using a regular expression, you can define patterns like:
- symbol followed by a number
- letter preceded by a hyphen
- a bunch of mixed characters in a particular order
- and and so many advanced patterns.
Regex for Validation
Regular expressions are not only for finding the parts of a string that matches a pattern. But they are also for validating that a string follows a pattern.
As you can see, email addresses are not straightforward. It can involve numbers, hyphens, and can be short or long. We can't do a search for this without regular expressions.
Developers also use regular expressions to validate that passwords meet a criteria. For example, you see in websites “your password must contain 1 letter, 1 number, and 1 symbol and must be 10 characters long”. You create a pattern for that, and test the password with the pattern, and you know if the password is valid or invalid.
There are so many other patterns you can create with Regular Expressions. It's not just the simple things like letter followed by number…no. As we progress in this course, you'd see the advanced patterns you can also write.
But there can be slight differences which depends on the language or the compiler of the language.
Topics We'll cover
This course is a playlist on YouTube. Here's the outline for the videos under the course:
For the first section of videos, we'll look at
For the second section, we'll look at how to create different patterns which involves:
- Character Classes
- Meta Characters
- Special Characters
- Capturing Groups
Then we wrap up.
- Wrap up
Multiple solutions for one pattern
One more thing I want you to understand is there can be different patterns for something you want to match. With regular expressions, there's no only one way. There can be thousands of ways. For example, my regex pattern for validating an email address may be different from yours.
One way might be more readable than another or shorter than other.