Skip to content

Instantly share code, notes, and snippets.

@caitlinramsey
Last active August 2, 2023 22:11
Show Gist options
  • Save caitlinramsey/b8a4c2ef9a32651caf3e18d604bbec15 to your computer and use it in GitHub Desktop.
Save caitlinramsey/b8a4c2ef9a32651caf3e18d604bbec15 to your computer and use it in GitHub Desktop.
Regular Expression Tutorial - Matching an Email

Regular Expression - Matching an Email

This tutorial is meant to breakdown the regular expression that would be used to validate an email. It is checking that the basic requirements for an email address are being met. The tutorial is supposed to make it easier for someone to understand why the regex looks the way it does and what each part is checking for.

Summary

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

  • The regex is considered a literal which is why it is wrapped in /.
  • The a-z is stating that the string can contain any lowercase letter between a-z.
  • The 0-9 is stating that the string can contain any number between 0-9.
  • The _\.- is showing that the string can contain an underscore, backslash, period, or hyphen.
  • The + is signifying that one or more occurrences of the preceding symbols is acceptable.
  • The . following, will match any character except the newline character which is \n.

Table of Contents

Regex Components

Anchors

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

The ^ at the beginning of the expression is an anchor which is signifying that the string begins with the characters that follow it. It has to be outside of the brackets, otherwise it takes on a different meaning. At then end of the expression, there is a(n) $ which is an anchor, just like the ^. However, the $ signifies that the string ends with the characters that precede it.

Quantifiers

([a-z\.]{2,6})

The {2,6} is signifying that there must be a minimum of 2 characters and a maximum of 6 characters.

  • Quantifiers can be defined in 3 different ways, { n }, { n, } and, like this one, { n, x }.

  • { n } will match the pattern exactly the number of times that n is defined as.

  • { n, } will match the pattern at least how many times n is defined as.

  • For this section of the regex, feb would be accepted since it is between 2 and 6 characters long as is lowercase.

Grouping Constructs

([a-z0-9_\.-]+)

The () are grouping a section of the regex known as a subexpression. This allows for the break up of different sections of the regex. This is helpful as it allows for checking for fulfillment of different requirements within a single string.

  • For this section of the regex, feb2- would be accepted since it is lowercase, the number is between 0 and 9, and the hyphen is acceptable.

Bracket Expressions

[a-z0-9_\.-]

The [] represent a range of characters that we are wanting to match. They are also known as a positive character group. Adding a ^ at the beginning of the expression but inside the bracket will turn the expression into a negative character group.

  • For this section of the regex, feb2- would be accepted since it is lowercase, the number is between 0 and 9, and the hyphen is acceptable.

Character Classes

([\da-z\.-]+)

The \d at the beginning of this subexpression is allowing the matching of any Arabic numeral digit. It is equivalent to writing [0-9] and the d stands for digit.

  • For this section of the regex, feb2- would be accepted since it is lowercase, the number is between 0 and 9 (which is what the \d is defining), and the hyphen is acceptable.

Author

Caitlin Ramsey is currently in a 6 month course learning how to be a full stack developer. This tutorial is meant to further the understanding of regular expressions.

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