Skip to content

Instantly share code, notes, and snippets.

@lexcraw4d
Created July 14, 2021 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexcraw4d/ed885a4b82fccda42d373f858f606c1e to your computer and use it in GitHub Desktop.
Save lexcraw4d/ed885a4b82fccda42d373f858f606c1e to your computer and use it in GitHub Desktop.
Regex - breaking down email example

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

Now that we have a general idea how Regex works (visit my simple Regex tutorial). Let's break down what's happening in the Email Regex example above.

The anchor used in this regex for email is the ^, which indicates the beginning of a string and the $, indicating the end of a string. Next, lets focus our attention to the (grouping/capturing which occurs between parenthesis). In group one, (remember group zero is the whole entity together as one) we see ([a-z0-9_\.-]). This matches the email name. Furthermore, we can tell because inside the brackets, it is matching any character in the range "a" to "z", any digits 0-9, underscore character which matches _ , the \ indicates an excape matching a . , and last the - character matching a -. Next, we noticed what every email has the @ symbol, which means without this matching, it wouldn't be an email! We move to our second group/capturing ([\da-z\.-]+). Inside our brackets here we see that this group matches any digit character (0-9), characters ranging from a-z,an escaped character matching the ., and the - character matching -. After our brackets we see a quantifier which matches one or more of the preceding token.

In between our capturing/groups two and three we see an escaped character\ which matches a .

In the third capturing/group we can take note of the brackets which allow us to again, match any characters within the set, that have a range of a-z, escaped by \ and matching a . character. Immediately following our [brackets] we see a {quantifier} notating a range {2,6} telling us to match between two and six of the preceding token.

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