What Is Regex? Beginner's Guide With Examples
Master regular expressions with our comprehensive cheat sheet. Learn character classes, quantifiers, anchors, flags, and common use cases for string matching.
Table of Contents
Quick Summary
What is Regex?
A regular expression (short for Regex or RegExp) is a string of text that allows you to create patterns that help match, locate, and manage text. Whether you are validating an email address on a frontend form, or parsing massive log files on a backend server, regex is the universally accepted standard for complex string matching.
Character Classes
Character classes match specific sets of characters. They are the building blocks of any expression.
.Matches any character except line breaks\wMatches any word character (alphanumeric & underscore)\WMatches any NON-word character\dMatches any digit (0-9)\DMatches any NON-digit\sMatches any whitespace character (spaces, tabs, line breaks)\SMatches any NON-whitespace character[abc]Matches exactly a, b, or c[^abc]Matches any character EXCEPT a, b, or c[a-z]Matches any character from a to z (lowercase)Quantifiers
Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.
*Matches 0 or more consecutive times+Matches 1 or more consecutive times?Matches 0 or 1 time (makes preceding token optional){n}Matches exactly n times{n,}Matches n or more times{n,m}Matches between n and m timesAnchors & Boundaries
Anchors do not match any characters. Instead, they match a position before, after, or between characters.
^Matches the start of a string$Matches the end of a string\bMatches a word boundary\BMatches a NON-word boundaryGroups & Lookarounds
(abc)Capturing group (groups multiple tokens together)(?:abc)Non-capturing group (groups tokens without saving for backreferencing)(?=abc)Positive Lookahead (matches a group after the main expression without including it)(?!abc)Negative Lookahead (specifies a group that can NOT match after the main expression)Flags / Modifiers
Flags change how the regular expression engine behaves.
- g (Global): Don't return after the first match. Find all matches.
- i (Case Insensitive): Match both upper and lower case letters.
- m (Multiline): Changes behavior of
^and$to match the start/end of a line instead of the whole string. - s (DotAll): Allows the dot (
.) character to match line breaks as well.
Common Real-World Patterns
Email Address Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Hex Color Code
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$UUID Validation
^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}Catastrophic Backtracking
Warning: Regular Expression Denial of Service (ReDoS)
(a+)+) are evaluated against a long string that almost matches but ultimately fails. The engine will check every possible combination, causing execution time to spike exponentially. Always test your patterns against both valid and invalid strings.Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
By default, quantifiers like * and + are greedy, meaning they match as much text as possible. Adding a question mark (e.g., *? or +?) makes them lazy, causing them to stop at the first possible match.
Is Regex the same across all programming languages?
Mostly, but not entirely. Different languages use different "engines" (e.g., PCRE in PHP/Perl, V8 in JavaScript). While standard character classes and quantifiers are universal, advanced features like lookbehinds or named capturing groups might have different syntax or browser support.
Try It Yourself
Test and debug your Regular Expressions instantly with real-time syntax highlighting.