Every regex metacharacter, anchor, quantifier, group, and lookaround — with practical examples. Test any pattern live with our interactive regex tester.
Jump to Practical PatternsAnchors don't match characters — they match positions in the string. They assert where a match can start or end.
| Token | Name | Meaning | Example |
|---|---|---|---|
^ | Start anchor | Start of string (or line in multiline mode) | ^Hello matches "Hello" only at the start |
$ | End anchor | End of string (or line in multiline mode) | world$ matches "world" only at the end |
\b | Word boundary | Position between word and non-word char | \bcat\b matches "cat" but not "catalog" |
\B | Non-word boundary | Position NOT at a word boundary | \Bcat matches "cat" in "catalog" but not " cat " |
^[A-Z] — Lines starting with a capital letter
\d$ — Lines ending with a digit
\b\w{6}\b — Exactly six-letter words
Quantifiers say how many of the preceding token to match. By default they are greedy — they match as much as possible.
| Token | Name | Meaning | Greedy? |
|---|---|---|---|
* | Star / Kleene star | 0 or more | Greedy |
+ | Plus | 1 or more | Greedy |
? | Optional | 0 or 1 | Greedy |
{n} | Exact | Exactly n times | — |
{n,} | At least | n or more | Greedy |
{n,m} | Range | Between n and m times | Greedy |
*? | Lazy star | 0 or more — as few as possible | Lazy |
+? | Lazy plus | 1 or more — as few as possible | Lazy |
?? | Lazy optional | 0 or 1 — prefers 0 | Lazy |
Text: "foo" "bar" "baz"
".+" (greedy) → matches "foo" "bar" "baz" — takes everything between first and last quote
".+?" (lazy) → matches "foo", "bar", "baz" — stops at each closing quote
| Token | Meaning | Negation |
|---|---|---|
\d | Digit [0-9] | \D — non-digit |
\w | Word char [a-zA-Z0-9_] | \W — non-word char |
\s | Whitespace [ \t\n\r\f\v] | \S — non-whitespace |
\t | Tab | — |
\n | Newline (LF) | — |
\r | Carriage return (CR) | — |
\\ | Literal backslash | — |
\. | Literal period (use \. not .) | — |
\xNN | Hex escape (e.g. \x20 = space) | — |
\\uNNNN | Unicode escape (JS: \\u00e9 = é) | — |
. (dot) matches any character except newline unless the s (dotall) flag is set. Always escape it as \. when matching a literal period.| Pattern | Meaning | Example |
|---|---|---|
[abc] | Any one of a, b, or c | gr[ae]y matches "gray" or "grey" |
[^abc] | NOT a, b, or c | [^0-9] matches any non-digit |
[a-z] | Range: a through z | [A-Za-z] any letter |
[a-zA-Z0-9_] | Same as \w | Word character |
[0-9] | Same as \d | Digit |
[.-.] | Escaped metachar inside class | [.\-\[\]] matches ., -, or [ |
[&&] | Intersection (Java) | [a-z&&[^aeiou]] consonants |
Most metacharacters lose their special meaning inside [...] — only \, ^ (if first), and - (if between two chars) need escaping.
q[^u] matches "q" followed by anything except "u" — useful for languages where "q" is always followed by "u".
| Token | Name | Description |
|---|---|---|
(...) | Capturing group | Groups a pattern AND captures the matched text. Referenced by \1, \2 (backreference) or $1, $2 (replacement). |
(?:...) | Non-capturing group | Groups a pattern but does NOT capture. No backreference created. |
(?P>name>...) | Named capture (Python) | Captures with a name. Reference: (?P=name) or \k<name> |
(?<name>...) | Named capture (.NET, JS, Ruby) | Captures with a name. JS: $<name> in replace. |
(?P=name) | Named backreference | Matches same text as the named group |
\1 .. \9 | Numeric backreference | Matches exact same text as group #1..9 earlier in the pattern |
(?(cond)yes|no) | Conditional | If group cond matched, try yes; otherwise try no |
(["']).*?\1 — Matches a quoted string with matching opening/closing quote. \1 must be the same character that group 1 captured (either " or ').
Works on: "hello", 'world' — but NOT on "broken'
(?:\d{3}-){2}\d{4} — Matches 555-123-4567 without storing the area code
Lookarounds are zero-width assertions that check what is (or isn't) ahead or behind the current position — without consuming characters.
| Token | Name | Matches |
|---|---|---|
(?=...) | Positive lookahead | Position followed by ... |
(?!...) | Negative lookahead | Position NOT followed by ... |
(?<=...) | Positive lookbehind | Position preceded by ... |
(?<!...) | Negative lookbehind | Position NOT preceded by ... |
\d+(?= dollars) — matches "100" in "100 dollars" but not "100 euros"
(?!.*\.exe$).+ — file names NOT ending in .exe
(?<=USD )\d+ — matches "50" in "USD 50" but not "EUR 50"
(?<!@)\w+ — words NOT preceded by @ (avoid matching usernames)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
Matches strings with at least 8 chars containing lowercase, uppercase, and a digit. Each lookahead checks a condition independently.
Flags change how the entire regex behaves. In JavaScript, they follow the pattern: /pattern/flags.
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | Treat a and A as the same |
m | Multiline | ^ and $ match line boundaries, not just string boundaries |
s | Dotall | . matches newline characters as well |
u | Unicode | Enables \u{...} escapes and treats the string as Unicode code points |
y | Sticky | Only matches from lastIndex (no scanning ahead) |
/hello/i — matches "Hello", "HELLO", "hello", "hElLo"
/^[A-Z]/gm — matches every line that starts with a capital letter
Ready-to-use regex patterns for real-world tasks. Test any of these live with our interactive regex tester. Validates most common email formats. Matches http/https URLs with optional www. Matches four-part decimal IP. Matches (123) 456-7890, 123-456-7890, etc. Matches YYYY-MM-DD format with validation. Matches opening or self-closing HTML tags. 8+ chars, upper, lower, digit, special. Standard 36-char hex UUID. Colon- or dash-separated hex pairs. #fff or #ffffff format. Lowercase letters, digits, hyphens. 16 digits with optional spaces/dashes. Captures the domain portion. Leading and trailing whitespace. Finds adjacent duplicate words. Standard base64 string validation. v1.2.3 or 1.2.3 with pre-release. Validates 00:00 through 23:59. XXX-XX-XXXX with valid area numbers. Full 40-char hex SHA or short 7-chars. Extract video ID from any YouTube URL. Matches valid CSS identifiers. Some engines (PCRE, Ruby) support recursive matching for nested structures: An atomic group Like greedy but NEVER gives back characters — same effect as atomic groups: Toggle flags mid-pattern with inline modifiers: .NET supports balanced groups for counting opening/closing delimiters: Copy any pattern from this cheat sheet into our interactive regex tester. See matches highlight in real time, inspect capture groups, and try substitutions./<div> blocks spanning multiple lines
8. Common Practical Patterns
Email Address
^[\w\.-]+@[\w\.-]+\.\w{2,}$
URL / Link
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
IPv4 Address
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$
Phone Number (US)
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Date (ISO 8601)
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
HTML Tag
<\/?[\w-]+(?:\s[^>]*)?\/?>
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_\-+=]).{8,}$
UUID / GUID
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$
MAC Address
^([0-9a-f]{2}[:-]){5}[0-9a-f]{2}$
Hex Color
^#([0-9a-f]{3}|[0-9a-f]{6})$
Slug (URL-friendly)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Credit Card (generic)
^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$
Extract Domain from URL
https?:\/\/([^\/\n]+)
Whitespace Trim
^\s+|\s+$
Duplicate Words
\b(\w+)\s+\1\b
Base64 Encoded
^[A-Za-z0-9+/]+={0,2}$
Semantic Version (SemVer)
^v?\d+\.\d+\.\d+(?:-[0-9a-z-]+)?$
Time (HH:MM 24h)
^(?:[01]\d|2[0-3]):[0-5]\d$
Social Security (US)
^(?!000|666)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$
Git Commit Hash
^[0-9a-f]{7,40}$
Youtube Video ID
(?:youtu\.be\/|youtube\.com\/(?:v\/|embed\/|watch\?v=))([\w-]{11})
CSS Class/ID Selector
^\.-?[_a-zA-Z][_a-zA-Z0-9-]*$
9. Advanced Techniques
Recursive Patterns
/\((?:[^()]|(?R))*\)/ # Matches nested parentheses
/\((?:[^()]|(?0))*\)/ # Same, alternative syntaxAtomic Groups
(?>...) prevents backtracking. Use it to fail faster and avoid catastrophic backtracking:(?>\d+)[a-z] # If no lowercase after digits, fail immediately
# instead of backtracking through every digitPossessive Quantifiers
\d++[a-z] # Possessive plus — never backtracks
\d*+[a-z] # Possessive star
\d?+[a-z] # Possessive optional(a+)+b or (x+x+)+y can lock up the engine on near-matches. Use atomic groups or possessive quantifiers to prevent this. Our regex tester catches this and shows the error.In-Engine Flags
(?i)case-insensitive # Turn on case-insensitivity from this point
(?-i)case-sensitive # Turn it off
(?i:insensitive only) # Only this part is case-insensitive
(?x) # Free-spacing mode (ignore whitespace and comments)Balanced Groups (.NET)
(?'open'<)(?:[^<>]|(?'open'<)|(?'-open'>))*(?(open)(?!))>Test These Patterns Live