Guides / Developer basics
What Is a Regular Expression? A Beginner Guide
A regular expression, or regex, is a short pattern that describes text. Programmers use regex to search, check and rewrite text in code, editors and spreadsheets.
The building blocks
- Literal text: cat matches the letters c, a and t.
- Character classes: \d matches a digit, \w matches a letter, digit or underscore, and [aeiou] matches any one listed character.
- Quantifiers: + means one or more, * means zero or more, and 4 means between two and four.
- Anchors: ^ matches the start of the text and $ matches the end. \b matches a word boundary.
Flags
Flags change how the pattern works. g finds every match rather than the first, i ignores upper and lower case, and m makes ^ and $ match at each line.
Why email patterns are tricky
Email addresses have many valid forms, and a short pattern will either reject some real addresses or accept some invalid ones. A simple pattern is fine for spotting a likely address in text. For checking that an address works, send a confirmation email instead.
Try it yourself
The Regex Tester highlights each match in your text as you type. Your text is not sent to a server.