Regex Cheat Sheet for Beginners
Regular expressions look like keyboard static until you learn the ~30 core symbols — then every pattern becomes readable. This cheat sheet covers character classes, quantifiers, anchors, and groups with plain-English explanations. Test each example live in our regex tester as you read.
Character classes: what to match
Character classes are the alphabet of regex — they define which characters a position can match. . matches any single character except a newline (so 'a.c' matches 'abc' but also 'a-c'). \d matches any digit (0-9), \D matches any non-digit. \w matches word characters — letters, digits, and underscore — while \W matches everything else. \s matches whitespace (spaces, tabs, newlines) and \S matches non-whitespace. Custom classes in square brackets match any one of the listed characters: [aeiou] matches a single vowel, [a-z] matches a lowercase letter, [0-9] matches a digit, and [^0-9] with the caret negates the class to match anything except a digit. Inside brackets, most special characters lose their magic — '.' inside [.] is a literal dot, which trips up beginners who over-escape. Paste '\d+' into our regex tester with the text 'Order 42 ships in 7 days' and watch it highlight 42 and 7 — that single experiment teaches more than a paragraph of theory. For extracting structured data like emails from real text, pair this with how to extract emails from text.
Quantifiers: how many to match
Quantifiers control repetition, and they are where regex gets its power — and its most confusing bugs. * means zero or more ('ab*c' matches 'ac', 'abc', 'abbbc'), + means one or more ('ab+c' requires at least one b), ? means zero or one ('colou?r' matches both 'color' and 'colour'). {n} means exactly n ('\d{3}' matches exactly three digits), {n,} means n or more, and {n,m} means between n and m ('\d{2,4}' matches 2 to 4 digits). The critical subtlety is greediness: by default, quantifiers match as much as possible. The pattern '<.*>' applied to 'bold and italic' matches the entire string from the first '<' to the last '>' — one giant match — because .* greedily consumed everything. Adding ? after the quantifier makes it lazy: '<.*?>' matches '' and '' separately. This greedy-vs-lazy distinction is the number one source of 'my regex matches too much' complaints. Test both versions in our regex tester against that HTML string and the difference becomes visceral. For counting the matches your pattern finds, our word counter helps quantify extraction results.
Anchors and boundaries: where to match
Anchors do not match characters — they match positions, which makes them the precision instruments of regex. ^ anchors to the start of the string (or line, with the multiline flag): '^\d+' matches digits only if they begin the string. $ anchors to the end: '\d+$' matches trailing digits. Together they enforce full-string validation: '^\d{5}$' matches exactly five digits and nothing else — the pattern behind ZIP code validation. \b is the word boundary, matching the position between a word character and a non-word character: '\bcat\b' matches 'cat' in 'the cat sat' but not in 'concatenate'. Beginners most often forget anchors in validation contexts: the pattern '\d{5}' without anchors 'validates' the string 'abc12345xyz' because it finds five digits somewhere inside — the classic validation bug. The fix is always the same: wrap validation patterns in ^ and $. The multiline flag (m) changes ^ and $ to match at line starts and ends within multi-line text, which matters when validating each line of a pasted list. Try '^\w+@' versus '\w+@' in our regex tester against a multi-line address list to feel the difference. For timestamped log lines where anchors meet real data, our Unix timestamp converter decodes the numbers your pattern extracts.
Groups, alternation, and capture
Groups organize patterns and extract data. (...) groups subpatterns and captures the matched text: '(\d{3})-(\d{4})' applied to '555-0123' captures '555' as group 1 and '0123' as group 2, which your code can reference separately. (?:...) is a non-capturing group — same grouping, no capture overhead — preferred when you only need the grouping for a quantifier or alternation. | is alternation (OR): 'cat|dog' matches either word, and combining with groups, '(mon|tues|wednes)day' matches three day names efficiently. Backreferences like \1 refer to a previously captured group within the same pattern: '(\w+) \1' matches a repeated word ('the the'), a genuinely useful proofreading trick. Named groups '(?
Flags and the 5 patterns worth memorizing
Flags modify the whole pattern's behavior. i makes matching case-insensitive ('cat' with the i flag matches 'CAT'), g finds all matches instead of stopping at the first, m enables multiline anchors, and s lets '.' match newlines too. Forgetting the g flag is the reason beginners see one match when they expected twenty. Now the five patterns worth memorizing, because they cover most real-world needs: email-ish: '[\w.+-]+@[\w-]+\.[a-z]+' — good enough for finding emails in text (true validation is stricter; see our dedicated guide). URL: 'https?://[^\s]+' — matches http/https links. ISO date: '^\d{4}-\d{2}-\d{2}$' — validates YYYY-MM-DD. Whitespace cleanup: '\s+' with replacement ' ' — collapses messy spacing. Repeated words: '\b(\w+) \1\b' — catches 'the the' typos. Memorize these five shapes and you will recognize their components in every pattern you ever read. Everything else is composition — and composition is exactly what our regex tester is for: paste the pattern, paste real text, and iterate until the highlights match your intent. When you are ready for the most-requested validation pattern, how to write regex for email validation goes deep.
Do it in one click
Test every pattern from this cheat sheet live — free regex tester.
Open the Free Tool →