Password Pattern Matching
Let's look at the rules and the conditions needed to solve the problem of password pattern matching.
We'll cover the following
If you’ve been reading up to this point, hopefully, you’re now used to thinking in RegExp terms, and you now understand the power that regular expressions bring to the table. This is exactly why they’re so widely used in all types of software projects.
In this final chapter, we’ll be showing you a few examples of real-world use cases where regular expressions can help you achieve some very nice functionalities with very few lines of code.
Rules for password
Password pattern matching is probably the easiest use case for regular expressions. After all, password rules tend to be a bit convoluted at times. How many times have you seen a message similar to this when trying to sign up to a website: “Your password needs to be between 6 and 12 characters long and should have, at least, an uppercase letter, a lowercase letter, a symbol, a number, and, ideally, a combination of letters that you’ll never be able to remember by yourself in a million years”?
Granted, that last bit might’ve come from our own frustration with these password rules. However, the first part of it is real. And, yes, there is always a way to implement the checking logic for these rules without regular expressions, but, then again, what would be the fun in that?
Although, at first glance, it might seem like a lot of checks for a single RegExp, we’ll be using a series of positive lookaheads (go back and read about quantifiers in chapter 2 if you don’t remember them) to check for each condition individually.
Essentially, we have five conditions to meet:
- (cond1): It needs to have at least one number.
- (cond2): It needs to have at least one lowercase letter.
- (cond3): It needs to have at least one uppercase letter.
- (cond4): It needs to have at least one symbol.
- (cond5): It needs to be between 6 and 12 characters long.
If we were to write a pseudocode to validate our string, it would look like this:
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy