Regex Cheatsheet for Beginners (With Copy-Paste Examples)
Regular expressions look cryptic at first, but a small set of building blocks covers most real-world tasks: validating input, searching logs, and doing bulk find-and-replace. Learn these pieces and you can read and write most patterns you'll meet. The basic building blocks . matches any single character except a newline. \d matches a digit, \w a word character (letters, digits, underscore), and…
Regular expressions can seem intimidating at first, but understanding a few key components allows you to tackle most real-world tasks. Some basic building blocks include the dot character ., which matches any character except a newline; \d for digits; \w for letters, digits, and underscores; and \s for whitespace. The capital versions \D, \W, and \S match the opposite.
Character classes let you match a group of options. For example, [abc] matches any single a, b, or c; [a-z] matches any letter; and [^abc] matches anything except a, b, or c. The ^ and $ characters match the beginning and end of a string, or the end of each line when using the m flag. The \b metacharacter stands for a word boundary—a handy check for whole-word matches.
To specify how many times a character or group appears, you can use quantifiers. An asterisk * means zero or more occurrences, a plus + means one or more, and a question mark ? means zero or one. Braces {3} specify exactly three occurrences, {2,5} between two and five, and {2,} two or more. Quantifiers are "greedy" by default, meaning they grab as many characters as they can. Adding a ? after one—like .*?—makes it "lazy," causing it to match as few characters as possible.
You can group parts of a pattern together with parentheses to create subpatterns, alternate between alternatives using the | symbol, and create backreferences to reuse parts of a match. For example, (cat|dog)s? will match cat, cats, dog, or dogs. Named capturing groups, like (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}), give meaningful names to parts of a pattern, which can be useful when extracting specific pieces later.
Lookahead and lookbehind assertions check what surrounds a position without including it in the match. Positive lookahead \d+(?= dollars) matches digits followed by the word "dollars," while positive lookbehind (?<! dollars)\d+ matches digits not followed by "dollars." The syntax for lookarounds is slightly different in various regex flavors.
Flags like g enable finding all matches rather than stopping at the first one; i makes the match case-insensitive; m causes ^ and $ to match at line boundaries; and s lets . also match newline characters. Common regex patterns include validating ISO dates, hexadecimal colors, and email addresses. However, regex alone cannot verify the actual existence of an email address or date. Always remember that regex validates shape, not truth.
When using regex, avoid common pitfalls such as forgetting to escape special characters, using greedy quantifiers when you need lazy ones, nesting quantifiers that lead to catastrophic backtracking, and assuming all engines behave identically. Start by testing your patterns against sample text, adding pieces one at a time, and observing the results. Many web-based regex testers allow you to experiment without uploading any files.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.