Common Regex Patterns

Ready-to-use regular expressions for common validation and extraction tasks

Email Address

Validation

Validates email addresses. Matches most common email formats.

Pattern:flags: i
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches:
user@example.comtest.email+tag@domain.co.uk
No match:
invalid@@nodomain.com

URL

Web & URLs

Matches HTTP and HTTPS URLs with various path and query string formats.

Pattern:
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Matches:
https://example.comhttp://www.test.org/path?query=1
No match:
ftp://invalid.comnot-a-url

US Phone Number

Validation

Matches US phone numbers in various formats including optional country code.

Pattern:
^\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
Matches:
(555) 123-4567555-123-4567
No match:
123-456555-1234-567890

International Phone

Validation

Matches international phone numbers in E.164 format.

Pattern:
^\+?[1-9]\d{1,14}$
Matches:
+14155551234+442071234567
No match:
+0123456789++1234567890

IPv4 Address

Validation

Validates IPv4 addresses with proper octet ranges (0-255).

Pattern:
^((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]?)$
Matches:
192.168.1.10.0.0.0
No match:
256.1.1.1192.168.1

IPv6 Address

Validation

Matches full IPv6 addresses (simplified pattern).

Pattern:flags: i
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Matches:
2001:0db8:85a3:0000:0000:8a2e:0370:7334fe80:0000:0000:0000:0000:0000:0000:0001
No match:
192.168.1.12001:db8::1

Date (ISO 8601)

Validation

Matches dates in ISO 8601 format (YYYY-MM-DD).

Pattern:
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Matches:
2024-01-152023-12-31
No match:
2024-13-012024-00-15

Date (US Format)

Validation

Matches dates in US format (MM/DD/YYYY).

Pattern:
^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$
Matches:
01/15/202412/31/2023
No match:
13/01/202401-15-2024

Time (24-hour)

Validation

Matches time in 24-hour format (HH:MM or HH:MM:SS).

Pattern:
^([01]\d|2[0-3]):([0-5]\d)(:([0-5]\d))?$
Matches:
14:3000:00
No match:
24:0014:60

Credit Card Number

Security

Validates major credit card numbers (Visa, MasterCard, Amex, Discover).

Pattern:
^(?: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})$
Matches:
41111111111111115500000000000004
No match:
1234567890123456411111111111

US Social Security Number

Security

Matches US Social Security Numbers in standard format.

Pattern:
^\d{3}-\d{2}-\d{4}$
Matches:
123-45-6789000-00-0000
No match:
123456789123-456-789

US ZIP Code

Validation

Matches US ZIP codes (5-digit or ZIP+4 format).

Pattern:
^\d{5}(-\d{4})?$
Matches:
1234512345-6789
No match:
1234123456

Strong Password

Security

Requires at least 8 characters with uppercase, lowercase, number, and special character.

Pattern:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Matches:
Passw0rd!Str0ng@Pass
No match:
passwordPASSWORD123

Username

Validation

Alphanumeric username with underscores and hyphens, 3-16 characters.

Pattern:
^[a-zA-Z0-9_-]{3,16}$
Matches:
john_doeuser123
No match:
abuser@name

Hex Color Code

Formatting

Matches hex color codes (3 or 6 digits, with or without #).

Pattern:
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Matches:
#fff#FFFFFF
No match:
#ffff#GGGGGG

URL Slug

Web & URLs

Matches URL-friendly slugs (lowercase letters, numbers, hyphens).

Pattern:
^[a-z0-9]+(?:-[a-z0-9]+)*$
Matches:
hello-worldmy-blog-post
No match:
Hello-Worldmy_slug

UUID

Validation

Matches valid UUID/GUID format.

Pattern:flags: i
^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Matches:
550e8400-e29b-41d4-a716-4466554400006ba7b810-9dad-11d1-80b4-00c04fd430c8
No match:
550e8400-e29b-41d4-a716not-a-uuid

MAC Address

Validation

Matches MAC addresses with colons or hyphens.

Pattern:
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
Matches:
00:1A:2B:3C:4D:5E00-1A-2B-3C-4D-5E
No match:
001A2B3C4D5E00:1A:2B:3C:4D

Extract Emails

Data Extraction

Extracts all email addresses from text.

Pattern:flags: g
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Matches:
Contact us at support@example.comEmail: john@test.org or jane@test.org
No match:
No emails hereinvalid@

Extract URLs

Data Extraction

Extracts all HTTP/HTTPS URLs from text.

Pattern:flags: g
https?:\/\/[^\s]+
Matches:
Visit https://example.com for moreLinks: http://a.com and https://b.com
No match:
No URLs hereftp://not-http.com

Extract Hashtags

Data Extraction

Extracts hashtags from social media text.

Pattern:flags: g
#[a-zA-Z0-9_]+
Matches:
Love this! #amazing #coolTrending: #javascript #coding
No match:
No hashtags here# space after

Extract @Mentions

Data Extraction

Extracts @mentions from social media text.

Pattern:flags: g
@[a-zA-Z0-9_]+
Matches:
Thanks @john and @jane!cc @team_lead
No match:
No mentions hereemail@domain.com

Extract Numbers

Data Extraction

Extracts all numbers (including decimals and negatives) from text.

Pattern:flags: g
-?\d+\.?\d*
Matches:
Price: $19.99Temperature: -5.5 degrees
No match:
No numbersabc def ghi

Extract HTML Tags

Data Extraction

Matches HTML/XML tags.

Pattern:flags: g
<[^>]+>
Matches:
<div>content</div><p class="text">Hello</p>
No match:
No tags here< not a tag >

Multiple Whitespace

Text Processing

Matches two or more consecutive whitespace characters.

Pattern:flags: g
\s{2,}
Matches:
too many spacesline\n\nnewlines
No match:
single spaces onlyno_spaces

Leading/Trailing Whitespace

Text Processing

Matches whitespace at the start or end of a string.

Pattern:flags: g
^\s+|\s+$
Matches:
leadingtrailing
No match:
no extra whitespace

Duplicate Words

Text Processing

Finds repeated consecutive words.

Pattern:flags: gi
\b(\w+)\s+\1\b
Matches:
the the quickis is this
No match:
no duplicates herethe quick brown

camelCase Word

Formatting

Matches camelCase formatted words.

Pattern:
^[a-z]+([A-Z][a-z]*)+$
Matches:
camelCasemyVariableName
No match:
PascalCasesnake_case

PascalCase Word

Formatting

Matches PascalCase formatted words.

Pattern:
^([A-Z][a-z]*)+$
Matches:
PascalCaseMyClassName
No match:
camelCasesnake_case

snake_case Word

Formatting

Matches snake_case formatted words.

Pattern:
^[a-z]+(_[a-z]+)*$
Matches:
snake_casemy_variable_name
No match:
camelCasePascalCase

File with Extension

Validation

Matches filenames with extensions.

Pattern:
^[\w,\s-]+\.[A-Za-z]{2,4}$
Matches:
document.pdfimage.jpeg
No match:
noextension.hidden

Image File Extension

Validation

Matches common image file extensions.

Pattern:flags: i
\.(jpg|jpeg|png|gif|bmp|svg|webp)$
Matches:
photo.jpglogo.PNG
No match:
document.pdfscript.js

Markdown Link

Data Extraction

Matches Markdown-style links [text](url).

Pattern:flags: g
\[([^\]]+)\]\(([^)]+)\)
Matches:
[Click here](https://example.com)[Link](url) and [another](url2)
No match:
[broken(link])no links here

JSON Key

Data Extraction

Extracts JSON object keys.

Pattern:flags: g
"([^"]+)"\s*:
Matches:
{"name": "value"}{"key1": 1, "key2": 2}
No match:
not json{key: value}