Developer Reference

Common Regex Patterns

Useful regular expressions for validation (email, phone, dates) and text processing.

Regular expressions (Regex) are sequences of characters that define a search pattern. They are extremely powerful for text validation, search, and replacement tasks.

Anchors

Start of string
^
End of string
$

Character Classes

Any single character
.
Digit (0-9)
\d
Word character (a-z, A-Z, 0-9, _)
\w
Whitespace (space, tab, newline)
\s

Groups

Any character in brackets
[abc]
Alternative (a or b)
(a|b)
Named Capture Group
(?<name>...)

Quantifiers

0 or more quantifiers
*
1 or more quantifiers
+
0 or 1 quantifier
?
Exactly 3 times
{3}

Security

Password: Min 8 chars, 1 letter, 1 number
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$

Formats

Date (YYYY-MM-DD)
\d{4}-\d{2}-\d{2}

Validation

URL validation
^https?:\/\/[\w\-]+(\.[\w\-]+)+
Email Address (Simple)
^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
IPv4 Address
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
Hex Color Code
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

Flags

Flag: Case insensitive mode
(?i)

Lookarounds

Positive Lookbehind: Match after "foo"
(?<=foo)
Negative Lookbehind: Match not after "foo"
(?<!foo)

Advanced

Atomic Group (No backtracking)
(?>...)
Backreference: Matches repeated groups
^(.*?)\1$
Negative Lookahead: String without "error"
(?!.*error)

Unicode

Unicode: Any letter
\p{L}
Unicode: Any number
\p{N}

Cleanup

Trailing whitespace
\s+$

While Regex can be cryptic, having a library of common patterns for emails, URLs, and dates can save you hours of debugging.