.NET/C# Regex Patterns Library
Explore our comprehensive collection of .NET/C# regular expression patterns. Find the perfect pattern for your needs or get inspired for your own regex solutions.
Email Validation
Validates email addresses with improved RFC 5322 compliance
Pattern
^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
Example
[email protected]
Explanation
.NET regex for email validation: - Use with Regex.IsMatch(email, pattern) - Or new Regex(pattern).IsMatch(email) - Supports compiled regex for better performance
URL Validation
Validates URLs with improved security and standards compliance
Pattern
^(https?:\/\/)?(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[\w\-\.~:/?#\[\]@!\$&'\(\)\*\+,;=%.]+)*$
Example
https://example.com/path?query=value#section
Explanation
.NET URL validation: - Regex.IsMatch(url, @"^(https?:\/\/)?(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[\w\-\.~:/?#\[\]@!\$&'\(\)\*\+,;=%.]+)*$") - Consider using Uri.TryCreate() for better validation
Phone Number
Validates international phone numbers in E.164 format
Pattern
^\+[1-9]\d{1,14}$
Example
+1234567890
Explanation
.NET regex for phone validation: - Regex.IsMatch(phone, @"^\+[1-9]\d{1,14}$") - Use verbatim strings (@"") to avoid escaping
Date Format (YYYY-MM-DD)
Validates dates in YYYY-MM-DD format
Pattern
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
Example
2024-03-20
Explanation
Requirements: - Year: 4 digits - Month: 01-12 - Day: 01-31 Note: This validates format only. For actual date validation, use a date parsing library.
ISO 8601 Date Time
Validates dates and times in ISO 8601 format
Pattern
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,3})?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$
Example
2024-03-20T15:30:45.123Z
Explanation
Requirements: - Date: YYYY-MM-DD - Time: HH:mm:ss - Optional milliseconds - Timezone: Z or ±HH:mm Note: This validates format only. For actual date validation, use a date parsing library.
Unix Timestamp
Validates Unix timestamps (seconds since epoch)
Pattern
^\d{10}$
Example
1710936000
Explanation
Requirements: - 10 digits - Seconds since Unix epoch (Jan 1, 1970) Note: This validates format only. For actual date validation, use a date parsing library.
Unix Timestamp (Milliseconds)
Validates Unix timestamps in milliseconds
Pattern
^\d{13}$
Example
1710936000000
Explanation
Requirements: - 13 digits - Milliseconds since Unix epoch Note: This validates format only. For actual date validation, use a date parsing library.
US Date Format (MM/DD/YYYY)
Validates dates in US format (MM/DD/YYYY)
Pattern
^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}$
Example
03/20/2024
Explanation
Requirements: - Month: 01-12 - Day: 01-31 - Year: 4 digits - Separator: / Note: This validates format only. For actual date validation, use a date parsing library.
European Date Format (DD/MM/YYYY)
Validates dates in European format (DD/MM/YYYY)
Pattern
^(?:0[1-9]|[12]\d|3[01])\/(?:0[1-9]|1[0-2])\/\d{4}$
Example
20/03/2024
Explanation
Requirements: - Day: 01-31 - Month: 01-12 - Year: 4 digits - Separator: / Note: This validates format only. For actual date validation, use a date parsing library.
RFC 2822 Date Format
Validates dates in RFC 2822 format
Pattern
^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\s\d{2}\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}\s(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d\s(?:[+-](?:[01]\d|2[0-3]):[0-5]\d|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])$
Example
Wed, 20 Mar 2024 15:30:45 +0000
Explanation
Requirements: - Day of week: Mon-Sun - Day: 01-31 - Month: Jan-Dec - Year: 4 digits - Time: HH:mm:ss - Timezone: ±HH:mm or abbreviation Note: This validates format only. For actual date validation, use a date parsing library.
Relative Date Format
Validates relative date expressions
Pattern
^(?:yesterday|today|tomorrow|last\s(?:week|month|year)|next\s(?:week|month|year)|\d+\s(?:days?|weeks?|months?|years?)\s(?:ago|from\snow))$
Example
2 days ago
Explanation
Requirements: - Common relative terms - Number + unit combinations - Direction indicators (ago/from now) Note: This validates format only. For actual date calculation, use a date parsing library.
Short Year Format (YY/MM/DD)
Validates dates with 2-digit years
Pattern
^(?:[0-9]{2})\/(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])$
Example
24/03/20
Explanation
Requirements: - Year: 2 digits - Month: 01-12 - Day: 01-31 - Separator: / Note: This validates format only. For actual date validation, use a date parsing library.
ISO Week Date Format
Validates dates in ISO week date format
Pattern
^\d{4}-W(?:0[1-9]|[1-4]\d|5[0-3])-[1-7]$
Example
2024-W12-3
Explanation
Requirements: - Year: 4 digits - Week: 01-53 - Day: 1-7 (Monday-Sunday) - Separator: - Note: This validates format only. For actual date validation, use a date parsing library.
Strong Password
Requires at least 12 characters with multiple character types
Pattern
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>])[A-Za-z\d!@#$%^&*()\-_=+{};:,<.>]{12,}$
Example
P@ssw0rd2024!
Explanation
.NET strong password validation: - Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>])[A-Za-z\d!@#$%^&*()\-_=+{};:,<.>]{12,}$") - Verbatim strings (@"") handle special characters well
Medium Strength Password
Requires at least 8 characters with multiple character types
Pattern
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$
Example
Passw0rd
Explanation
Requirements: - Length: ≥8 characters - Uppercase: ≥1 - Lowercase: ≥1 - Numbers: ≥1
Basic Password
Requires at least 6 characters with at least one letter and one number
Pattern
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$
Example
pass123
Explanation
Requirements: - Length: ≥6 characters - Letters: ≥1 - Numbers: ≥1
Complex Password
Requires at least 12 characters with specific character type requirements
Pattern
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>])[A-Za-z\d!@#$%^&*()\-_=+{};:,<.>]{12,}$
Example
P@ssw0rd2024!
Explanation
Requirements: - Length: ≥12 characters - Uppercase: ≥1 - Lowercase: ≥1 - Numbers: ≥1 - Special chars: ≥1 Note: Consider using a password strength estimator for better security.
Minimal Password
Requires at least 4 characters with at least one letter
Pattern
^(?=.*[A-Za-z])[A-Za-z\d]{4,}$
Example
pass
Explanation
Requirements: - Length: ≥4 characters - Letters: ≥1 Note: This is a very weak password policy and should be avoided in production.
Password Without Spaces
Requires at least 8 characters with no spaces allowed
Pattern
^[^\s]{8,}$
Example
Password123
Explanation
Requirements: - Length: ≥8 characters - No spaces allowed
Alphanumeric Password
Requires at least 8 characters with only letters and numbers
Pattern
^[A-Za-z\d]{8,}$
Example
Password123
Explanation
Requirements: - Length: ≥8 characters - Only letters and numbers allowed
Password with Max Length
Requires 8-20 characters with at least one letter and one number
Pattern
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$
Example
Password123
Explanation
Requirements: - Length: 8-20 characters - Letters: ≥1 - Numbers: ≥1 Note: Modern security guidelines recommend against maximum length restrictions.
IPv4 Address
Validates IPv4 addresses
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]?)$
Example
192.168.1.1
Explanation
Matches valid IPv4 addresses with: - Four octets (0-255) - Dots as separators - No leading zeros Note: This validates format only. For network validation, use additional checks.
IPv6 Address
Validates IPv6 addresses
Pattern
^(?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$
Example
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Explanation
Matches valid IPv6 addresses with: - Eight 16-bit hexadecimal groups - Groups separated by colons - Optional compression of consecutive zero groups - Optional IPv4-mapped format - Optional zone index for link-local addresses Note: This validates format only. For network validation, use additional checks.
Credit Card Number
Validates credit card numbers
Pattern
^(?:4[0-9]{12}(?:[0-9]{3}){0,2}|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9]|9[0-9])[0-9]{11,14}|6(?:011|4[4-9][0-9]|5[0-9]{2})[0-9]{12,15}|35(?:2[8-9]|[3-8][0-9])[0-9]{12,15}|62[0-9]{14,17})$
Example
4111111111111111
Explanation
Matches major credit card formats: - Visa (4): 13, 16, or 19 digits - Mastercard (51-55, 2221-2720): 16 digits - Amex (34, 37): 15 digits - Diners Club (300-305, 36, 38, 39): 14-19 digits - Discover (6011, 644-649, 65): 16-19 digits - JCB (3528-3589): 16-19 digits - UnionPay (62): 16-19 digits Note: Must be followed by Luhn algorithm check.
Visa Card
Validates Visa card numbers
Pattern
^4[0-9]{12}(?:[0-9]{3}){0,2}$
Example
4111111111111111
Explanation
Matches Visa card numbers that: - Start with 4 - Are 13, 16, or 19 digits long Note: Must be followed by Luhn algorithm check.
Mastercard
Validates Mastercard numbers
Pattern
^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$
Example
5105105105105100
Explanation
Matches Mastercard numbers that: - Start with 51-55 or 2221-2720 - Are exactly 16 digits long Note: Must be followed by Luhn algorithm check.
American Express Card
Validates American Express card numbers
Pattern
^3[47][0-9]{13}$
Example
378282246310005
Explanation
Matches American Express card numbers that: - Start with 34 or 37 - Are exactly 15 digits long Note: Must be followed by Luhn algorithm check.
Diners Club Card
Validates Diners Club card numbers
Pattern
^3(?:0[0-5]|[68][0-9]|9[0-9])[0-9]{11,14}$
Example
30569309025904
Explanation
Matches Diners Club card numbers that: - Start with 300-305, 36, 38, or 39 - Are 14-19 digits long Note: Must be followed by Luhn algorithm check.
Discover Card
Validates Discover card numbers
Pattern
^6(?:011|4[4-9][0-9]|5[0-9]{2})[0-9]{12,15}$
Example
6011111111111117
Explanation
Matches Discover card numbers that: - Start with 6011, 644-649, or 65 - Are 16-19 digits long Note: Must be followed by Luhn algorithm check.
JCB Card
Validates JCB card numbers
Pattern
^35(?:2[8-9]|[3-8][0-9])[0-9]{12,15}$
Example
3530111333300000
Explanation
Matches JCB card numbers that: - Start with 3528-3589 - Are 16-19 digits long Note: Must be followed by Luhn algorithm check.
Union Pay Card
Validates Union Pay card numbers
Pattern
^62[0-9]{14,17}$
Example
6212345678901234
Explanation
Matches Union Pay card numbers that: - Start with 62 - Are 16-19 digits long Note: Must be followed by Luhn algorithm check.
Hipercard
Validates Hipercard numbers
Pattern
^(?:384100|384140|384160|606282|637095|637568|637599|637609|637612|60(?!11)[0-9]{4})(?:[0-9]{8}|[0-9]{10})$
Example
3841001234567890
Explanation
Matches Hipercard numbers that: - Start with specific BIN ranges (384100, 384140, etc.) or 60xxxx (not 6011xx) - Are 14 or 16 digits long Note: Must be followed by Luhn algorithm check.
Maestro Card
Validates Maestro card numbers
Pattern
^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$
Example
5018123456789012
Explanation
Matches Maestro card numbers that: - Start with 50, 56-58, 6304, 6390, or 67 - Are 12-19 digits long Note: Must be followed by Luhn algorithm check.
Laser Card
Validates Laser card numbers
Pattern
^(?:6304|6706|6771|6709)[0-9]{12,15}$
Example
6706123456789012
Explanation
Matches Laser card numbers that: - Start with 6304, 6706, 6771, or 6709 - Are 16-19 digits long Note: Laser is a defunct card scheme.
Solo Card
Validates Solo card numbers
Pattern
^(6334|6767)[0-9]{12}|(6334|6767)[0-9]{14}|(6334|6767)[0-9]{15}$
Example
6334123456789012
Explanation
Matches Solo card numbers that: - Start with 6334 or 6767 - Are 16, 18, or 19 digits long Note: Solo is a defunct card scheme.
Switch Card
Validates Switch card numbers
Pattern
^(?:4903|4905|4911|4936|6333|6759)[0-9]{12}|(?:4903|4905|4911|4936|6333|6759)[0-9]{14}|(?:4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$
Example
4903123456789012
Explanation
Matches Switch card numbers that: - Start with 4903, 4905, 4911, 4936, 6333, or 6759 and are 16, 18, or 19 digits - Start with 564182 and are 16, 18, or 19 digits - Start with 633110 and are 16, 18, or 19 digits Note: Switch is a defunct card scheme.