Skip to content

Instantly share code, notes, and snippets.

@avuong19
Created June 19, 2022 08:53
Show Gist options
  • Save avuong19/282161e09b62f3e2ca9f3f96734e9400 to your computer and use it in GitHub Desktop.
Save avuong19/282161e09b62f3e2ca9f3f96734e9400 to your computer and use it in GitHub Desktop.
Regex Tutorial: Matching an email

Regex Tutorial: Matching an email

This is a tutorial about Regex, or regular expression. What is Regex? Regex is a string of text that helps us create patterns in order to search for text. The following document will walk you through how to use Regex to match an email address.

Summary

We will use this Regex below for this example

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

Break down

/: Begin Regex ^: Match any string it start with ( and ) : Create a group [ and ]: Begin and End bracket list [a-z0-9_\.-]+: This will match any letters and numbers. The a-z means any letters between the two. The 0-9 means any numbers between 0 and 9. It also accepts characters _. -, and \. means escaping sequence denoting the character .. + means any of those characters can be repeated more than one @: This is the @ required for an email format [\da-z\.-]+: This will take any letters. Also \d means it will match a single character that is a digit. \. means escaping sequence denoting the character ..+ means any of those characters can be repeated more than one \.: the . before any extension [a-z\.]{2,6}: This means it can take any letters from a-z with the total minimum length of 2 and maximum length of 6 $: anchor at the end of the string

Table of Contents

Regex Components

Anchors

The anchors stay at the begining and the end of the string. As we can see from the example above, we have ^ and $

  • ^: match any string that start with. For example ^an, any string starts with an such as anything will be a match
  • $: match any string that end with. For example er$, any string that ends with er such as teacher will be a match

Quantifiers

Quantifiers define specific requirements of an input for a match to appear. In the example above, we have + and {2,6} are representations of what quantifiers are

  • +: means one or more. For example, xyz+ means that a string has to have xy and follow by one ore more z.
  • {2,6}: provides a minimum and maximum characters. This is considered a fixed quantifier.

OR Operator

This is used to match THIS or THAT

  • | is used as an OR operator to indicate that it can be matched with one or more patterns. For example this|that: a string that has either this or that

Character Classes

This is used to include any character match requirement

  • [a-z]: match all letters between a-z Some other common ones that are not used in the example above
  • \d: match a single character that is a digit
  • .: match all characters
  • \w: match a word character

Flags

At the end of a regex, we can put a flag to dictate some value. For example,

  • i: means insensitive, which ignores case-sensitive letters
  • g: means global, which will not return the first match but also start subsequent search at the end of the 1st match
  • and so on...

Grouping and Capturing

This help organize characters to extract information needed

  • (,): used to group characters

Bracket Expressions

Expression that use brackets

  • [,]: used to define character class. For example [a-z] means any string that has at least one letter range between a to z

Greedy and Lazy Match

A greedy match try to get as many matches as possible while a lazy match will try to only get one match Some common examples are :

  • * + and {}

Boundaries

This helps with whole words search if you need to find an exact match to a certain string

  • \b : for example \bhi\b, this will search for the word hi exactly how it is spelled out within the boundary

Look-ahead and Look-behind

Look ahead:

  • ?=: for example a(b?=) means it will only match with a if the a is followed by a b, but the b is not a part of the regex match Look behind:
  • ?<=: for example a(b?<=) means it will only match with a if the a has a b right before, but the b is not a part of the regex match

Author

Reference material: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 My name is Anh Vuong. I am a student of UT coding bootcamp seeking a career in web development. Here is my github: https://github.com/avuong19

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