This regex tutorial will help you learn and understand how REGEX works otherwise known as regular expressions. regular expressions are powerfull tools used for pattern matching. Regex can be used to check password strength verification, email verification, Url matching, and Data extraction.
I will be going over how Regex is used to find and exrtract a specific email starting with anchors and then ending with Character Escapes at the end of this tutorial you should firmly be able to understand how regex syntactically works in order to find a specific email.
- Anchors
- Quantifiers
- Grouping Constructs
- Bracket Expressions
- Character Classes
- The OR Operator
- Flags
- Character Escapes
/^
([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$
/
the ^
specify the start of the string and the $
denotes the end of the regex string.
/^([a-z0-9_.-]+
)@([\da-z.-]+
).([a-z.]{2,6}
)$/
What is a quantifier? Well the name "Quantifier" is pretty self explanitory, Quantifiers are used to specifiy how many instances of a character, group or character class must be present in the input for a match to be found. The symbol for our regex quantifier is the +
sign meaning anything to the left of the +
sign must inlude those characters it insures that the user email does not remain empty. this => {2,6}
Quantifier explains that the preceding character class [a-z\.]
should allow 2 to 6 lower case letters or dots. For Quantifiers there are five basic constructs being greedy, docile, lazy, helpful, and possessive by default our Quantifier is greedy meaning that our Quantifier will match one or more instances for our proceeding character group.
/^([a-z0-9_\.-]+)
@([\da-z\.-]+)
.([a-z\.]{2,6})
$/
Grouping Constructs capture what is required in that specific part of your regular expression. The first grouping is your name the second after the @
is the domain like google, yahoo, firefox, and so on. The third group is the extension for example .com, .co, org, ect.
/^([a-z0-9_\.-]
+)@([\da-z\.-]
+).([a-z\.]
{2,6})$/
Bracket Expressions specify what should be included inside of your Grouping Constructs. In more detail [a-z0-9_\.-]
picks what should be inside the name of your email from a to z and 0 to 9 including underscores periods and hyphens. after your @
[\da-z\.-]
this literal specifys that you can choose any character from 0-9 from the \d
, after that it lets you choose any letter from a to z and just like the prevous allows you to choose periods or hyphens but exludes the option of a underscore. .([a-z\.]
{2,6}) this grouping then contains a character escape that that forces the regex to check for a period after your domain name like gmail.com then only includes letters a through z and unlike last time this grouping only allows periods. This Quantifier {2,6} explains that it only allows a character to repeat between two to six times.
/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/
\d
is present in the given matching email code and what it will match a single letter character, a-z, after the @
sign in the email address. Basically ensuring that a letter is matched after the @
in the email and not a number or special character.
/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/
This regex contains no OR Operator What is an OR operator? An OR Operator allows you to specify alternatives within a pattern. you can tell when a regular expression contains an OR Operator when you these characters being used in this way [yes|no], [apple|potato] if the strings that you are looking for match yes or no apple or potato your regex will find one or the other. It is important to note that within the regex engine in more complexe regular expressions it will stop at the first alternative matched so keep on eye on the order of your alternatives.
/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/
this regex contains no Flags
What is a Flag? A flag is an optional parameter that alters the behavior of regex pattern matching. Flags are found at the end of a regex litteral and affect how the regex engine applies the pattern. For example if you wanted to make your pattern case sensitive matching both upper case and lower case letters it would look something like this /pattern/i
.
/^([a-z0-9_\.
-]+)@([\d
a-z\
.-]+)\.
([a-z\.
]{2,6})$/
Character Escapes are represented by backslashes in our case we have two character escapes \.
and \d
. For our first character escape \.
this is to allow a literal dot. The second character escape \d
is to allow any character from [0-9]
Hello my name is Ian, if you like my gist and would like to see more go to my github profile to get a peek on some of my other activities.
github profile => https://github.com/Ian-kensington-chadwick-the-3rd