Regex Cheat Sheet: Complete Regular Expression Reference

A complete regex quick reference: character classes, quantifiers, anchors, groups, lookaheads, flags, and 15 common ready-to-use patterns for emails, URLs, dates, and more.

NK
Nitin KaushikPublished 15 November 2025 · Updated 1 June 2026 · 10 min read

Advertisement

Regular expressions (regex) are patterns used to match, search, and manipulate text. The syntax is compact but can feel cryptic until you've internalized the building blocks. This cheat sheet is your quick reference for everything from basic character matching to advanced lookaheads, with 15 ready-to-use patterns for common validation tasks.

Test your regex

Use our free Regex Tester to build and test regular expressions against sample text. It highlights all matches, shows groups, and flags errors in real time.

Test Regex in Your Browser — Free

Build, test, and debug regular expressions with live match highlighting.

Open Regex Tester →

Character Classes

Regex character classes

PatternMatchesExample
.Any character except newlinea.c matches 'abc', 'a1c', 'a c'
\dDigit [0-9]\d+ matches '42', '100'
\DNon-digit [^0-9]\D+ matches 'abc', 'hello'
\wWord char [a-zA-Z0-9_]\w+ matches 'hello_world'
\WNon-word character\W+ matches ' !@#'
\sWhitespace (space, tab, newline)\s+ matches spaces
\SNon-whitespace\S+ matches non-space runs
[abc]Any of: a, b, or c[aeiou] matches any vowel
[^abc]NOT a, b, or c[^0-9] matches non-digits
[a-z]Character range a through z[a-zA-Z] matches any letter
[\w\s]Word chars or whitespaceMatches words with spaces

Advertisement

Quantifiers

Regex quantifiers

QuantifierMeaningExample
*0 or more (greedy)a* matches '', 'a', 'aaa'
+1 or more (greedy)a+ matches 'a', 'aaa' (not '')
?0 or 1 (optional)colou?r matches 'color' and 'colour'
{n}Exactly n times\d{4} matches exactly 4 digits
{n,}n or more times\d{3,} matches 3+ digits
{n,m}Between n and m times\d{2,4} matches 2–4 digits
*?0 or more (lazy)a*? matches minimal 'a's
+?1 or more (lazy)a+? matches minimal 'a's
??0 or 1 (lazy)a?? prefers 0 matches

Anchors

Regex anchors

AnchorMatchesExample
^Start of string (or line with m flag)^Hello matches 'Hello world'
$End of string (or line with m flag)world$ matches 'Hello world'
\bWord boundary\bcat\b matches 'cat' not 'concatenate'
\BNon-word boundary\Bcat\B matches 'cat' in 'concatenate'
\AStart of string (Python/Java)Absolute string start
\ZEnd of string (Python/Java)Absolute string end

Groups and Alternation

Regex groups and alternation

SyntaxMeaningExample
(abc)Capturing group — stores the match(\d+) captures number
(?:abc)Non-capturing group — groups without storing(?:Mr|Mrs)\.
(?P<name>abc)Named capturing group (Python/PCRE)(?P<year>\d{4})
(?<name>abc)Named capturing group (JS/PCRE2)(?<year>\d{4})
a|bAlternation: a OR bcat|dog matches 'cat' or 'dog'
\1Backreference to group 1(\w+) \1 matches 'hello hello'

Advertisement

Lookaheads and Lookbehinds

Regex lookaheads and lookbehinds

SyntaxMeaningExample
(?=abc)Positive lookahead: followed by abc\d+(?= dollars) matches numbers before 'dollars'
(?!abc)Negative lookahead: NOT followed by abc\d+(?! dollars) excludes dollar amounts
(?<=abc)Positive lookbehind: preceded by abc(?<=\$)\d+ matches digits after '$'
(?<!abc)Negative lookbehind: NOT preceded by abc(?<!\$)\d+ excludes dollar amounts

Flags / Modifiers

Common regex flags

FlagNameEffect
iCase-insensitive/hello/i matches 'Hello', 'HELLO', 'hello'
gGlobalFind all matches (not just first)
mMultiline^ and $ match start/end of each line
sDotall (singleline). matches newlines as well
xExtended (verbose)Allows whitespace and comments in pattern
uUnicodeEnable Unicode matching (/\u{1F600}/u)

Common Regex Patterns

Ready-to-use regex patterns

PatternMatchesRegex
EmailBasic email format[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}
URL (http/https)Web URLshttps?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-\._~:/?#[\]@!$&'()*+,;=]*
Phone (US)US phone number^(\+1\s?)?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$
Date (YYYY-MM-DD)ISO date^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
IPv4 addressIP address^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Hex colour#RGB or #RRGGBB^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
UUID v4UUID format[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
Postal code (UK)UK postcode^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$
Strong password8+ chars, mixed^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$
Credit card (Visa)Visa card number^4[0-9]{12}(?:[0-9]{3})?$
HTML tagAny HTML tag<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([\s\S]*?)<\/\1>
SlugURL-safe slug^[a-z0-9]+(?:-[a-z0-9]+)*$
UsernameAlphanumeric + _-^[a-zA-Z0-9_\-]{3,16}$
Whitespace-onlyBlank or whitespace^\s*$
Non-empty stringAt least one char\S+

Regex in Different Languages

Regex usage in common programming languages

LanguageTest a matchFind all matches
JavaScript/pattern/flags.test(str)str.match(/pattern/g) or [...str.matchAll(/pattern/g)]
Pythonre.search(pattern, str)re.findall(pattern, str)
JavaPattern.compile(p).matcher(s).matches()Matcher.find() in a loop
PHPpreg_match('/pattern/', $str)preg_match_all('/pattern/', $str, $matches)
Goregexp.MustCompile(p).MatchString(s)regexp.MustCompile(p).FindAllString(s, -1)
C# / .NETRegex.IsMatch(str, pattern)Regex.Matches(str, pattern)
Rubystr.match?(/pattern/)str.scan(/pattern/)

Test Your Regex Patterns Free

Build and debug regex with live match highlighting. Works for JavaScript and PCRE syntax.

Open Regex Tester →

Advertisement

Frequently Asked Questions

What does .* mean in regex?

. means any character (except newline by default). * means 0 or more of the preceding element. Together .* means 'zero or more of any character' — it matches everything on a line. .+ requires at least one character.

What is the difference between * and + in regex?

* means 0 or more occurrences (it's optional). + means 1 or more occurrences (at least one required). \d* matches '' or '42' or '1234'. \d+ only matches '42' or '1234', not ''.

How do I match an exact string in regex?

For an exact full-string match, anchor the pattern with ^ at the start and $ at the end: ^Hello world$. Without anchors, the pattern can match a substring anywhere in the text.

How do I make a regex case-insensitive?

Add the 'i' flag: /hello/i matches 'hello', 'Hello', 'HELLO'. In Python: re.compile(pattern, re.IGNORECASE). In Java: Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).

What is a capturing group in regex?

A capturing group ( ) stores the text matched by the subpattern inside the parentheses. In JavaScript, match[1] gives you the first group. Use (?:) for non-capturing groups if you want grouping without storage overhead.

How do I escape special characters in regex?

Prepend a backslash \. These regex metacharacters must be escaped: . * + ? ^ $ { } [ ] | ( ) \. To match a literal dot, use \. To match a literal parenthesis, use \( and \).

Related Tools