Pattern syntax, common patterns, and quick examples
| . | Any character except newline |
| \d | Digit [0-9] |
| \D | Non-digit [^0-9] |
| \w | Word char [a-zA-Z0-9_] |
| \W | Non-word char |
| \s | Whitespace [ \t\n\r\f\v] |
| \S | Non-whitespace |
| [abc] | Any of a, b, or c |
| [^abc] | Not a, b, or c |
| [a-z] | Range: a through z |
| [a-zA-Z0-9] | Alphanumeric |
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 (optional) |
| {3} | Exactly 3 |
| {3,} | 3 or more |
| {3,5} | Between 3 and 5 |
| *? | 0 or more (lazy) |
| +? | 1 or more (lazy) |
| ?? | 0 or 1 (lazy) |
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Non-word boundary |
| \A | Start of string (always) |
| \Z | End of string (always) |
| (abc) | Capture group |
| (?:abc) | Non-capturing group |
| (?<name>abc) | Named capture group |
| \1 | Backreference to group 1 |
| \k<name> | Backreference by name |
| a|b | Alternation: a or b |
| (a|b)c | ac or bc |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| (?<=abc) | Positive lookbehind |
| (?<!abc) | Negative lookbehind |
| \n | Newline |
| \t | Tab |
| \r | Carriage return |
| \\ | Literal backslash |
| \. | Literal dot |
| \* \+ \? | Literal *, +, ? |
| \( \) | Literal parentheses |
| \[ \] | Literal brackets |
| \{ \} | Literal braces |
| g | Global -- all matches, not just first |
| i | Case-insensitive |
| m | Multiline -- ^ and $ match line boundaries |
| s | Dotall -- . matches newline too |
| u | Unicode support |
| x | Extended -- ignore whitespace, allow comments |
| [:alpha:] | Letters [a-zA-Z] |
| [:digit:] | Digits [0-9] |
| [:alnum:] | Alphanumeric |
| [:space:] | Whitespace |
| [:upper:] | Uppercase [A-Z] |
| [:lower:] | Lowercase [a-z] |
| [:punct:] | Punctuation |
| $0 or $& | Entire match |
| $1, $2 ... | Capture group 1, 2 ... |
| ${name} | Named group |
| $` | Before match |
| $' | After match |
Email (simplified)
[\w.+-]+@[\w-]+\.[\w.]+
URL
https?:\/\/[\w\-._~:/?#\[\]@!$&'()*+,;=%]+
IPv4 Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Phone (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Time (HH:MM, 24h)
([01]\d|2[0-3]):[0-5]\d
Hex Color
#([0-9a-fA-F]{3}){1,2}\b
HTML Tag
<([a-z]+)([^<]*?)(?:>(.*?)<\/\1>|\s*\/>)
Slug (URL-safe)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Strong Password
(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}
Username (3-16 chars)
^[a-zA-Z0-9_-]{3,16}$
Credit Card (basic)
\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b