Tuesday, October 4
Shadow

Regex Email matching tutorial and breakdown

# How to validate Emails with Regex

Here is a tutorial on the use of regex to match emails using the expression `/^([a-z_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,9})$/i`. This can be very helpful when working with email validation in tech such as Node or MongoDB.

## Summary

This is a tutorial that will be covering the basics of using regex for email validation through matching. Regex is a regular expression which is a sequence of characters used to define search parameters within code and databases using patterns. We will be utilizing `/^([a-z_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,9})$/i` to check emails.

## Table of Contents

- [Anchors](#anchors)
- [Quantifiers](#quantifiers)
- [OR Operator](#or-operator)
- [Character Classes](#character-classes)
- [Flags](#flags)
- [Grouping and Capturing](#grouping-and-capturing)
- [Bracket Expressions](#bracket-expressions)
- [Greedy and Lazy Match](#greedy-and-lazy-match)
- [Boundaries](#boundaries)
- [Back-references](#back-references)
- [Look-ahead and Look-behind](#look-ahead-and-look-behind)


## Example Regex

### Regex
`/^([a-z_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,9})$/i`

### Emails to check
- somecoolguy@yahoo.com
- totallyrealemail@gmail.com
- professionalperson@outlook.com
- imrunningoutofideas@yahoo.com
- mylastidea@gmail.com

## Regex Components

### Anchors

The anchors used to match an email using the above example are:
- `^ `, indicates the beginning of the string
- `$`to represent the ending of the string.
- It does not include `(m)`, causing the Regex to end at `$`.

### Quantifiers

Quantifiers in this regex are `+` which connects the username + email provider + and site extensions, and `{2,9}` which limits site extensions to a minimum of 2 characters and a maximum of 9 characters.

### OR Operator
 
Operators are not being utilized in this tutorial, However an OR operator is an expression part that searches for specific options and selects if one of them is caught in the search, examples are but not limited to: `( gov | edu | com )` which would seach for those tags at the end of a website URL.

### Character Classes

The character class we are utilizing in this expression is `\d` which is used to search for single digits from `0-9` in the email providers name.

### Flags

At the end of the expressions outisde of the litteral string indicators `//` we have included `i` which allows us to ignore case sensitivity and search with a parameter of `[a-z]` instead of `[a-zA-Z]`.

### Grouping and Capturing

This regex expression contains 3 groups consiting of the username `([a-z_\.-]+)`, the address provider `([\da-z\.-]+)` and the site extension `([a-z\.]{2,9})`

### Bracket Expressions

We also use 3 bracket expressions in the 3 groups username `[a-z_\.-]`, the address provider `[\da-z\.-]` and the site extension `[a-z\.]`. Each of these expressions contained within `[]` are searching for anything in a string that matches those characters without case sensitivity due to our `//i` flag.

### Greedy and Lazy Match

This regex uses both greedy and lazy matches. The `+` quantifier acts as a greedy match, taking everything it can from the string before continuing on to the next part of the regex. The groups in the `()` are lazy matching because as soon as the string hits a symbol it does not know it stops and moves on in the regex.

### Boundaries

The boundaries we use are also utilized as our anchors. `^`, and `$` define the start and end of our string.

### Back-references

Like our boundary, `$` it is also used as our back reference. At the end of the regex we include `$` as it shows the end of the string as well as collecting everything from its group. Since we have not given `$` a group number to pull it defaults to, `$0` prints the whole thing and stops at that point, thus collecting our whole email.

### Look-ahead and Look-behind

Look-ahead & Look-behind were not used in this tutorial.

## Author

Leave a Reply