Regex

Essential regular expression patterns, syntax, and examples for text matching and manipulation.

languages
regexregular-expressionspatternstext-processing

Basic Patterns

.           Match any single character except newline
\d          Match any digit [0-9]
\D          Match any non-digit
\w          Match word character [a-zA-Z0-9_]
\W          Match non-word character
\s          Match whitespace (space, tab, newline)
\S          Match non-whitespace

Character Classes

[abc]       Match a, b, or c
[^abc]      Match anything except a, b, or c
[a-z]       Match any lowercase letter
[A-Z]       Match any uppercase letter
[0-9]       Match any digit
[a-zA-Z]    Match any letter
[a-zA-Z0-9] Match any alphanumeric character

Quantifiers

*           0 or more
+           1 or more
?           0 or 1 (optional)
{n}         Exactly n times
{n,}        n or more times
{n,m}       Between n and m times
*?          Lazy: match as few as possible
+?          Lazy: match as few as possible

Anchors

^           Start of string/line
$           End of string/line
\b          Word boundary
\B          Non-word boundary

Groups & Capturing

(abc)       Capturing group
(?:abc)     Non-capturing group
(?<name>x)  Named capturing group
\1          Backreference to group 1
\k<name>    Backreference to named group
(a|b)       Match a or b

Lookahead & Lookbehind

(?=foo)     Positive lookahead
(?!foo)     Negative lookahead
(?<=foo)    Positive lookbehind
(?<!foo)    Negative lookbehind

Flags

i           Case insensitive
g           Global (find all matches)
m           Multiline mode
s           Dotall (. matches newlines)
u           Unicode support

Escape Sequences

\\          Literal backslash
\.          Literal dot
\*          Literal asterisk
\?          Literal question mark
\+          Literal plus
\^          Literal caret
\$          Literal dollar sign
\[          Literal bracket
\{          Literal brace
\|          Literal pipe
\(          Literal parenthesis
\t          Tab
\n          Newline
\r          Carriage return

Common Patterns

Email (simple)
[\w.-]+@[\w.-]+\.\w{2,}

URL
https?:\/\/[\w.-]+(?:\/[\w.\/?%&=-]*)?

Phone (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

IP Address (IPv4)
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Date (YYYY-MM-DD)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])

Time (HH:MM:SS)
(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d

Hex Color
#(?:[0-9a-fA-F]{3}){1,2}\b

Username (3-16 alphanumeric)
^[a-zA-Z0-9_]{3,16}$

Strong Password (8+ chars, mixed)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$

ZIP Code (US)
\b\d{5}(?:-\d{4})?\b

JavaScript Usage

// Test for match
/pattern/.test(string)

// Find first match
string.match(/pattern/)

// Find all matches
string.match(/pattern/g)

// Replace first
string.replace(/pattern/, 'replacement')

// Replace all
string.replace(/pattern/g, 'replacement')

// Split by pattern
string.split(/pattern/)

// Capture groups
const match = string.match(/(\w+)@(\w+)/)
// match[1] = first group, match[2] = second group

Python Usage

import re

# Search anywhere in string
re.search(r'pattern', string)

# Match at start only
re.match(r'pattern', string)

# Find all matches
re.findall(r'pattern', string)

# Replace
re.sub(r'pattern', 'replacement', string)

# Split
re.split(r'pattern', string)

# Compile for reuse
pattern = re.compile(r'pattern')
pattern.search(string)

Examples

Match email
Input: "Contact us at hello@example.com"
Pattern: [\w.-]+@[\w.-]+\.\w+
Match: "hello@example.com"

Match repeated words
Input: "the the quick brown"
Pattern: \b(\w+)\s+\1\b
Match: "the the"

Extract numbers
Input: "Price: $19.99"
Pattern: \d+\.?\d*
Matches: "19.99"

Validate hex color
Input: "#ff5733"
Pattern: ^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$
Match: "#ff5733"

Match HTML tags
Input: "<div>content</div>"
Pattern: <(\w+)>.*?<\/\1>
Match: "<div>content</div>"
  • Regex Tester - Test and debug your regular expressions interactively