Regex Cheat Sheet for Backend Developers

A quick-reference sheet for the regex symbols, character classes, and validation patterns that come up constantly in backend work — API input validation, log parsing, and data cleaning. Everything here is a static reference; if you'd rather describe what you need in plain English and have a pattern generated for you, use the free Regex Generator instead.

Basic Regex Symbols

  • . — any character except a newline
  • * — zero or more of the preceding token
  • + — one or more of the preceding token
  • ? — zero or one (makes the preceding token optional)
  • {n,m} — between n and m repetitions
  • ^ / $ — start / end of string
  • | — alternation ("or")
  • () — capturing group; (?:) — non-capturing group

For lookahead/lookbehind and greedy-vs-lazy quantifiers — the two things that trip up most people past the basics — see regex lookahead and lookbehind explained and non-greedy vs greedy matching.

Character Classes

[abc]      -> a, b, or c
[a-z]      -> any lowercase letter
[A-Z]      -> any uppercase letter
[0-9]      -> any digit
[^0-9]     -> anything that is NOT a digit
\d         -> digit, shorthand for [0-9]
\w         -> word character, shorthand for [A-Za-z0-9_]
\s         -> whitespace (space, tab, newline)

Validation Pattern Reference

What it matchesPattern
Email address^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
Username (3-16 chars, letters/digits/underscore)^[A-Za-z0-9_]{3,16}$
Strong password (8+ chars, upper, lower, digit)^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
IPv4 address (each octet 0-255)^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Hex color code^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
URL (http/https)^https?:\/\/[\w.-]+\.[a-z]{2,}(\/\S*)?$
Indian mobile number^(\+91)?[6-9]\d{9}$
Indian PIN code^[1-9]\d{5}$

The URL pattern above requires an explicit http(s):// prefix; for a version that also accepts bare domains, www., localhost, and IP addresses with ports, see regex for URL validation in JavaScript. For Aadhaar, PAN, GST, IFSC, passport, and driving license patterns — plus which of these regex can and can't fully verify — see the complete guide to Indian ID and document regex validation. For 50 more copy-paste patterns organized by category, see 50 ready-to-use regex patterns for developers.

Using These Patterns in Code

// Node.js
const phoneRegex = /^(\+91)?[6-9]\d{9}$/;
phoneRegex.test("9876543210"); // true

# Python
import re
pattern = r"^(\+91)?[6-9]\d{9}$"
bool(re.match(pattern, "9876543210"))  # True

Java and JavaScript both need the pattern wrapped as a string literal with backslashes escaped; Python's raw string prefix (r"...") lets you paste a pattern as-is without doubling the backslashes.

Common Backend Use Cases

  • Validating API request bodies before they hit business logic
  • Enforcing password strength rules at signup
  • Extracting structured fields (IPs, error codes, timestamps) out of log lines
  • Parsing or cleaning malformed CSV rows before import
  • Validating identifiers like PAN, GST, or SKU codes on form submission

Regex Patterns Interviewers Commonly Ask You to Write

These come up often enough in backend interview rounds that they're worth being able to write from memory, not just recognize:

  • Email validation — see the pattern in the table above.
  • Find duplicate consecutive words\b(\w+)\s+\1\b, using a backreference (\1) to check the same word appears twice in a row.
  • Extract all numbers from a string\d+ with the global flag, e.g. "order 12 has 3 items".match(/\d+/g) in JavaScript.
  • Enforce password rules — see the strong-password pattern in the table above, which uses lookahead assertions to require multiple character classes without fixing their order.

Practicing writing these from scratch is more useful than memorizing them — try the Regex Generator with your own prompt, then compare what it produces to what you'd have written by hand.

Frequently Asked Questions

What is the difference between this cheat sheet and the Regex Generator?

This page is a quick static reference — symbols, character classes, and copy-paste patterns you can scan in a few seconds. The free Regex Generator instead takes a plain-English description ("match a valid email") and builds a new pattern for you on demand, with a live tester attached. Use the cheat sheet when you already roughly know what you need; use the generator when you don't want to write the regex by hand at all.

Do these patterns work the same in Java as in Node.js and Python?

Mostly yes — the core syntax (character classes, quantifiers, anchors) is shared across PCRE-style regex engines including JavaScript, Python's re module, and Java's java.util.regex. The differences that trip people up are usually around escaping: Java and JavaScript need patterns wrapped in a string literal with backslashes escaped, while Python's raw string prefix (r"...") lets you paste a pattern as-is.

Is regex validation enough for Aadhaar, PAN, or GST numbers?

Regex can confirm a string has the right shape (correct length, correct character positions) but cannot verify a checksum digit. PAN and GST numbers both use format rules regex can check; Aadhaar has an internal checksum that regex alone cannot validate. See the dedicated guide to Indian ID and document regex validation for which formats need more than a regex check.

How do I test these patterns before using them in production?

Paste the pattern and a sample string into the built-in tester on the Regex Generator page, or use the Regex Explainer to get a token-by-token breakdown of what a pattern actually matches before you trust it against real data.

Generate or Test a Pattern Instead of Writing One by Hand

Describe what you need in plain English and get a working pattern with a live tester attached — free, no signup.

Related articles