Hoja de Referencia Regex
Referencia rápida para expresiones regulares: anclas, clases de caracteres, cuantificadores, grupos, lookarounds, flags y patrones listos.
Anchors
| Pattern | Matches |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary (between \w and non-word) |
\B | Non-word boundary |
\A / \Z | Start / end of string (PCRE only — not JS) |
Character classes
| Pattern | Matches |
|---|---|
. | Any character except newline (or any with s flag) |
\d | Digit [0-9] |
\D | Non-digit |
\w | Word char [A-Za-z0-9_] |
\W | Non-word char |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Range a through z |
[a-zA-Z0-9] | Multiple ranges |
Quantifiers
| Pattern | Matches |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | 2 to 5 |
{2,} | 2 or more |
*? +? ?? | Lazy (match as little as possible) |
*+ ++ ?+ | Possessive (no backtracking — PCRE) |
Groups & references
| Pattern | Matches |
|---|---|
(abc) | Capture group 1 |
(?:abc) | Non-capturing group |
(?<name>abc) | Named group (use \k<name> to backreference) |
\1 \2 | Backreference to group 1, 2 |
a|b | a or b (alternation) |
Lookarounds
| Pattern | Matches |
|---|---|
(?=abc) | Followed by abc (positive lookahead) |
(?!abc) | NOT followed by abc (negative lookahead) |
(?<=abc) | Preceded by abc (positive lookbehind) |
(?<!abc) | NOT preceded by abc (negative lookbehind) |
Flags
| Flag | Effect |
|---|---|
g | Global — find all matches, not just first |
i | Case-insensitive |
m | Multiline — ^ and $ match line boundaries |
s | Dotall — . matches newline too |
u | Unicode mode |
y | Sticky (JavaScript) — match at lastIndex only |
Common ready-to-use patterns
| Use case | Pattern |
|---|---|
| Email (basic) | ^[\w.+-]+@[\w-]+\.[\w.-]+$ |
| URL (http/https) | ^https?://[^\s/$.?#].[^\s]*$ |
| IPv4 | ^(\d{1,3}\.){3}\d{1,3}$ |
| Indian phone | ^(\+91[\-\s]?)?[0]?(91)?[6-9]\d{9}$ |
| UUID v4 | ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| ISO 8601 date | ^\d{4}-\d{2}-\d{2}$ |
| Hex color | ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ |
| Slug (URL-friendly) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| PAN (India) | ^[A-Z]{5}[0-9]{4}[A-Z]$ |
| GSTIN (India) | ^\d{2}[A-Z]{5}\d{4}[A-Z]\d[Z]\d$ |
Test any pattern interactively in our Regex Tester.
Regex Cheat Sheet — Complete Regular Expression Reference with Ready-to-Use Patterns
Regular expressions are one of the highest-leverage skills in software development — a single well-written regex can replace 50 lines of string parsing code. Yet most developers use only a small fraction of what regex can do, defaulting to basic patterns while complex validation, extraction, and transformation tasks pile up. This cheat sheet covers the full regex toolkit: anchors, character classes, quantifiers, groups, lookarounds, flags, and a library of ready-to-use patterns for common validation tasks including Indian-specific formats like PAN and GSTIN.
How to use this cheat sheet
Scan the section that matches your problem — anchor, character class, quantifier, group, or lookaround. Copy the pattern, then test it live in our Regex Tester to verify it against your actual input. The patterns in this reference work in JavaScript and PCRE flavors. Where there are differences between flavors (like lookbehind variable-length support or possessive quantifiers), the notes column calls it out. The "Common ready-to-use patterns" section at the bottom is a direct copy-paste library — email validation, URL matching, IPv4, Indian phone numbers, UUID, ISO dates, hex colours, URL slugs, PAN, and GSTIN patterns are all tested and ready to use.
The four pillars of regex you must know
Anchors (^ $ \b) control where matches are allowed to occur in the string — without anchors, a pattern matches anywhere in the input including partial matches. Character classes (\d \w \s . [abc] [^abc]) define what characters are acceptable at each position. Quantifiers (* + ? {n,m} and their lazy variants) control how many times a character or group can repeat. Groups and alternation ((abc) (?:abc) a|b) allow you to match sequences as units, capture substrings for extraction, and provide multiple valid pattern alternatives. Everything else in regex — lookarounds, backreferences, flags — is built on these four foundations. Master these and you can construct any regex you need.
Who uses this cheat sheet
Backend developers validating form input use it to find or verify patterns for email, phone, postal code, and ID format validation. Data engineers writing ETL pipelines use it for text extraction and transformation patterns on log data, CSV files, and API responses. Security researchers use it when writing WAF rules or analysing log patterns for intrusion detection. Front-end developers building form validation use the ready-to-use patterns section as a starting point and customise from there. Developers working on Indian applications specifically use it for PAN number and GSTIN validation patterns that are hard to find correctly written elsewhere.
Privacy and data handling
This is a static reference page — no data is collected or transmitted. Test patterns against your own data using the linked Regex Tester, which also runs entirely in your browser.
Preguntas frecuentes
- How do I learn regex fast?
- Memorize the four pillars: anchors (^ $ \b), character classes (\d \w \s . []), quantifiers (* + ? {n,m}), and groups (). Everything else builds on these. Test every pattern as you learn it — rote memorization without practice fails fast. Spend 30 minutes a day for two weeks and you'll cover 95% of real-world cases.
- What's the difference between PCRE, JavaScript, and POSIX regex?
- PCRE (Perl-compatible) is the richest dialect — supports lookbehind, named groups, recursion. JavaScript regex is similar to PCRE but lacks variable-length lookbehind in older browsers. POSIX is older, mostly used in grep/sed/awk; lacks lookarounds entirely. Most online regex testers default to PCRE or JavaScript flavor.
- When should I NOT use regex?
- Parsing HTML, XML, or JSON — use proper parsers. Matching balanced brackets/parens — regex can't count. Validating email addresses for delivery — use proper email validation libraries that check MX records. The 'if it can be parsed' rule: if the language has a defined grammar, use a parser, not regex.
- Why is .* so slow on long input?
- It's catastrophic backtracking. Greedy quantifiers like .* try every possible match before giving up. On long inputs with nested quantifiers (e.g., (a+)+), regex can take exponential time. Solutions: use atomic groups (?>...), possessive quantifiers (*+, ++), or anchor with lookarounds. Re-write to avoid nested optionality.
- How do I match a literal special character?
- Escape it with a backslash. Special chars in regex: . * + ? ^ $ ( ) [ ] { } | \ /. To match a literal dot: \. To match a literal backslash: \\. Inside character classes [] most special chars lose meaning except - ] \ ^. So [.] matches a literal dot without escaping needed inside the class.
Herramientas relacionadas
- Formateador JSON con Validación y Reparación
Formatea, minifica, valida y repara JSON al instante en tu navegador. Ordena claves alfabéticamente, formato automático al pegar, descarga como archivo — gratis, sin registro, 100% en el cliente.
- Generador de Código QR
Genera códigos QR para URLs, texto, Wi-Fi y más. Descarga como PNG.
- Generador de Contraseñas
Genera contraseñas fuertes y aleatorias con longitud y conjuntos de caracteres personalizados.
- Codificador / Decodificador Base64
Codifica texto a Base64 o decodifica Base64 de vuelta a texto plano.
- Codificador / Decodificador de URL
Codifica o decodifica URLs y cadenas de consulta con codificación por porcentaje.