50 Regex Patterns Every Developer Should Know

Essential regular expressions for validation, parsing, and text manipulation

Updated January 2025 · 20 min read

Regex Basics Refresher

Before diving into patterns, let's review the essential regex syntax:

CharacterMeaningExample
.Any charactera.c matches "abc", "a1c"
*Zero or moreab* matches "a", "ab", "abbb"
+One or moreab+ matches "ab", "abbb"
?Zero or onecolou?r matches "color", "colour"
^Start of string^hello
$End of stringworld$
\dDigit (0-9)\d{3} matches "123"
\wWord character\w+ matches "hello"
\sWhitespace\s+ matches spaces
[abc]Character class[aeiou] matches vowels
(a|b)Alternation(cat|dog)
{n,m}Quantifier\d{2,4} matches 2-4 digits

Email Validation Patterns

1. Basic Email

Simple email validation for most use cases

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
✓ user@example.com · ✓ test.name+tag@domain.co.uk · ✗ invalid-email

2. Simple Email (Quick Check)

Minimal validation - just checks for @ and domain

^\S+@\S+\.\S+$
✓ a@b.c · ✗ no-at-sign.com

URL & Web Patterns

3. URL with HTTP/HTTPS

Validates web URLs with optional www

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
✓ https://example.com · ✓ http://www.test.com/path?q=1

4. URL Slug

SEO-friendly URL slugs

^[a-z0-9]+(?:-[a-z0-9]+)*$
✓ my-blog-post · ✓ article-123 · ✗ Invalid_Slug

5. Domain Name

Validates domain names without protocol

^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$
✓ example.com · ✓ sub.domain.co.uk

6. IPv4 Address

Validates IP addresses (0-255 per octet)

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
✓ 192.168.1.1 · ✓ 10.0.0.1 · ✗ 256.1.1.1

Phone Number Patterns

7. US Phone Number

Matches (123) 456-7890, 123-456-7890, 123.456.7890

^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$
✓ (555) 123-4567 · ✓ 555.123.4567

8. International Phone (E.164)

International format with country code

^\+?[1-9]\d{1,14}$
✓ +14155551234 · ✓ +442071234567

Test Your Regex Patterns

Use our free regex tester to validate patterns and see matches in real-time.

Open Regex Tester →

Date & Time Patterns

9. ISO 8601 Date (YYYY-MM-DD)

International standard date format

^\d{4}-\d{2}-\d{2}$
✓ 2025-01-27 · ✗ 01/27/2025

10. US Date (MM/DD/YYYY)

US date format with validation

^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/\d{4}$
✓ 01/27/2025 · ✗ 13/01/2025

11. 24-Hour Time

HH:MM format (00:00 to 23:59)

^([01]?[0-9]|2[0-3]):[0-5][0-9]$
✓ 23:59 · ✓ 9:30 · ✗ 24:00

12. ISO 8601 DateTime

Full datetime with timezone

^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})$
✓ 2025-01-27T14:30:00Z · ✓ 2025-01-27T14:30:00+05:30

Password Validation

13. Strong Password

Min 8 chars, uppercase, lowercase, number, special char

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
✓ MyP@ssw0rd · ✗ password · ✗ Short1!

14. Medium Password

Min 8 chars, uppercase, lowercase, number

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$
✓ Password1 · ✗ password1

Numbers & Currency

15. Integer

Positive or negative whole numbers

^-?[0-9]+$
✓ 123 · ✓ -456 · ✗ 12.34

16. Decimal Number

Numbers with optional decimal places

^-?[0-9]*\.?[0-9]+$
✓ 123.456 · ✓ .5 · ✓ -0.5

17. USD Currency

US dollar amounts with optional commas

^\$[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
✓ $19.99 · ✓ $1,234.56

18. Percentage

Percentage values with decimals

^-?[0-9]+(?:\.[0-9]+)?%$
✓ 50% · ✓ 99.99% · ✓ -5%

IDs & Codes

19. UUID v4

Standard UUID format

^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$
✓ 550e8400-e29b-41d4-a716-446655440000

20. Credit Card Number

Visa, MasterCard, Amex, Discover

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$
✓ 4111111111111111 (Visa) · ✓ 5500000000000004 (MC)

21. US ZIP Code

5-digit or ZIP+4 format

^[0-9]{5}(?:-[0-9]{4})?$
✓ 12345 · ✓ 12345-6789

22. Hex Color Code

3 or 6 digit hex colors

^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
✓ #FF5733 · ✓ #F53 · ✓ FF5733

23. MAC Address

Network MAC address

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
✓ 00:1A:2B:3C:4D:5E · ✓ 00-1A-2B-3C-4D-5E

Text Processing Patterns

24. Extract Hashtags

Find Twitter/social media hashtags

#[a-zA-Z0-9_]+
✓ #javascript · ✓ #web_dev

25. Extract @Mentions

Find user mentions

@[a-zA-Z0-9_]+
✓ @username · ✓ @user_123

26. Remove Multiple Spaces

Find 2+ consecutive spaces

{2,}
Replace with single space to normalize text

27. HTML Tags

Match HTML tags (for stripping)

<[^>]+>
✓ <div> · ✓ <p class="text">

Code & Development Patterns

28. Semantic Version

SemVer format (major.minor.patch)

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?$
✓ 1.0.0 · ✓ 2.1.3-beta.1

29. Git Commit Hash

Short or full commit SHA

^[0-9a-f]{7,40}$
✓ a1b2c3d · ✓ a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2

30. JWT Token

JSON Web Token format

^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$
✓ eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature

Pro Tip: Always test regex patterns with edge cases. A pattern that works for common inputs may fail on unusual but valid data.

Multi-Language Examples

JavaScript

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

// Test
emailRegex.test('user@example.com'); // true

// Extract matches
const text = 'Contact us at info@example.com or support@example.com';
const emails = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g);
// ['info@example.com', 'support@example.com']

Python

import re

email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

# Test
if re.match(email_pattern, 'user@example.com'):
    print('Valid email')

# Find all
text = 'Contact info@example.com or support@example.com'
emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
# ['info@example.com', 'support@example.com']

PHP

$pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';

// Test
if (preg_match($pattern, 'user@example.com')) {
    echo 'Valid email';
}

// Find all
$text = 'Contact info@example.com or support@example.com';
preg_match_all('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/', $text, $matches);
// $matches[0] = ['info@example.com', 'support@example.com']

Explore More Regex Patterns

Browse our library of 100+ ready-to-use regex patterns with detailed explanations.

View Regex Library →

Related Resources