Sometime you need some help to search or validate entries in your code that matches certain of pattern, It can cause so much deception because the amount of code to accomplish the task. That is Regex (Regular Expressions) comes to help us to minimize our job.
Regex is a sequence of characters that define a search pattern. Such patterns are used by searching for one or more that matches of a specific string or for input validation
It will present a Password Validation with Regular Expressions (Regex)
Regex: ^(?=.*\d)(?=.*[a-zA-Z])(?=.*[A-Z])(?=.*[-\#\$\.\%\&\*])(?=.*[a-zA-Z]).{8,16}$
The password must match:
- At least 8 - 16 characters,
- must contain at least 1 uppercase letter,
- must contain at least 1 lowercase letter,
- and 1 number
- Can contain any of this special characters $ % # * & - .
It is written in 5 Look-ahead groups that matches what is looking for:
-
Second part:
^ ... .{8,16}$
the Dot and the Quantifier {8, 16}. That describes any of the preceding (that matches the password requirements ) characters limited with 8 to 16 characters long. -
Third part:
(?=.*\d)(?=.*[a-zA-Z])(?=.*[A-Z])(?=.*[-\#\$\.\%\&\*])(?=.*[a-zA-Z])
. It has 5 groups-
First group:
(?=.*\d)
. Look-ahead that matches the a set of any characters (.*
) without show them to the result and following of digits. -
Second, Third and Fifth group:
(?=.*[a-zA-Z])(?=.*[A-Z]) ... (?=.*[a-zA-Z])
. Look-ahead that matches the a set of any characters (.*
) without show them to the result and following of any uppercase and lowercase letters. -
Fourth Group:
(?=.*[-\#\$\.\%\&\*])
. Look-ahead that matches the a set of any characters (.*
) without show them to the result and following of a special characters- # $ . % & *
. Notice each special character has before them a Class called backslash\
except the dash symbol-
-
Note: the ...
sequential points are examples to explain the step by step the Regex
^ : Beginning, Matches the beginning of the string
$ : End, Matches the end of the string
* : Star, Matches 0 or more of the preceding characters
+ : Plus, Matches 1 or more of the preceding characters
{8, 16} : Specific Quantifier, Matches from 8 to 16 of the preceding characters
. : Dot, Matches any character except linebreaks
[] : Characters set / OR Operator / Bracket Expressions, Match any character in the set
\d : Digit, Matches any digit character (0-9)
\ : backslash, operator to capture special characters such as $ % # * & - .]
() : Capturing Group, Groups multiple tokens together and creates a capture group
(?= ) : Look-ahead, Matches a group after the main expression without including it in the result
Full Stack Web Developer Jr.
GitHub profile Rogers0404: https://github.com/rogers0404.
Try it : Try By yourself on regexr.com.
Check it live on Github live https://rogers0404.github.io/passwordValidation/.
Github Repository: [GIT: [email protected]:rogers0404/passwordValidation.git.
Check this post, here I validate the minimum cases.
a capital letter
a lowercase
A number
A symbol
From 8 to 50 characters
https://luisenriquech.blogspot.com/2022/07/expresion-regular-contrasena.html