Skip to content

Instantly share code, notes, and snippets.

@noahneville
noahneville / regex-email-tutorial.md
Last active May 12, 2022 18:22
Finding An Email Address with Regex: A Regular Expressions Tutorial

Finding An Email Address with Regex: A Regular Expressions Tutorial

Regular Expressions, often abbreviated Regex, are a powerful tool used to find, validate, replace and split strings of text. In this tutorial, we'll scratch the surface of what regexes can do by using one, step by step, to find all email addresses in a body of text.

Summary

First of all, what is a regular expression? In it's simplest form, a regular expression is just a string of text, used to define a search pattern for text. Technically, "banana" is a regular expression. In this case, using it in a search would yield results for that exact set of characters in that case. Each character is a "literal character", meaning that the pattern is literally lowercase "b", lowercase "a", etc.

Literal characters are very limited as a tool because they only find words you already know exactly. But what makes regular expressions so powerful is the addition of meta-characters, which are characters that indicate a generalized pattern rather tha