Skip to content

Instantly share code, notes, and snippets.

@Tristan-Ballin
Last active November 30, 2022 00:50
Show Gist options
  • Select an option

  • Save Tristan-Ballin/6fb71d0c148621203a300bb5c3265ee8 to your computer and use it in GitHub Desktop.

Select an option

Save Tristan-Ballin/6fb71d0c148621203a300bb5c3265ee8 to your computer and use it in GitHub Desktop.
Regex for an email

Matching an Email: A Tutorial

A regex, which is short for regular expression, is a sequence of characters that defines a specific search pattern. When included in code or search algorithms, regular expressions can be used to find certain patterns of characters within a string, or to find and replace a character or sequence of characters within a string. They are also frequently used to validate input.

Summary

I will be teaching you how to make a regex for an email validation and describing which parts of the regex describe what.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

Table of Contents

Regex Components

Anchors

The ^ anchor signifies a string that begins with the characters that follow it. And the $ anchor signifies a string that ends with the characters that precede it. In our example we can see our expression enclosed with those anchors /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

Quantifiers

Quantifiers set the limits of the string that your regex, or an individual section of it, matches. it can be a single character like * or it could be multiple characters inside curly brackets like {3} indicating a size of 3 or {3,16} indicating a size between 3 and 16 just like in our example.

Grouping and Capturing

In order to make a more complex regex you will need to group sections of it with different requirements. The way to group is to use parenthesis (). Everything in the parenthesis will have the same requirements for that section of the expression. In our example, /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ you can see that we have 3 different groups seperated by some requirements ([a-z0-9_\.-]+) & ([\da-z\.-]+) need to be seperated by @ and ([\da-z\.-]+) & ([a-z\.]{2,6}) need to be seperated by .

Bracket Expressions

Bracket expressions or defined using brackets [] and represents a range of characters that we want to match. They are used to include all of the characters we want to match. for example [a-z] matches all lowercase letters and [0-9] matches all numbers. They can also include special characters like in our example [a-z0-9_\.-] represents all lowercase letters, numbers, and underscores, periods and dashes.

Greedy and Lazy Match

Greedy and Lazy matches refer to how we choose the string being affected by each group. A greedy match will try the longest possible string and a lazy match will try the shortest possible string. Our expression uses greedy methods on each group which is indicate by the + at the end of the groups or by the quantifier {3,16} which is inherently greedy.

Character Escapes

Character escapes are represented with a backslash \ and are used for characters that are used to define the regex itself but are wanted as a requirement inside the regex. For example, the open curly brace { is used to begin a quantifier, but adding a backslash before the open curly brace \{ means that the regex should look for the open curly brace character instead of beginning to define a quantifier.

Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment