The article demonstrates useful regex pattern examples. All examples are written in C#. You can find the regex patterns to validate: hex colors, daytime, email, phone number, ZIP code, strong password, MD5 hash, latitude and longitude pairs.
The regex pattern for CSS 6-digit hex colors
To validate CSS 6-digit hex colors, use the regex pattern "^#{0,1}[a-fA-F0-9]{6}$"
It validates colors with the “#” prefix and without it.
using System.Text.RegularExpressions;
string pattern = @"^#{0,1}[a-fA-F0-9]{6}$";
Regex regex = new Regex(pattern);
string correctColor = "FFC0CB";
Match match = regex.Match(correctColor);
Console.WriteLine($"The color {correctColor} is {(match.Success ? "correct" : "wrong")}");
// Result: The color FFC0CB is correct
The regex pattern for CSS 8-digit hex colors
To validate CSS 8-digit hex colors, use the regex pattern "^#{0,1}[a-fA-F0-9]{8}$"
It validates colors with the “#” prefix and without it.
Note: The CSS 8-digit hex color consists of a hash symbol (#) and eight characters. The first six characters represent the RGB (red, green, blue) value of the color, and the last two represent the alpha channel of the color.
using System.Text.RegularExpressions;
string pattern = @"^#{0,1}[a-fA-F0-9]{8}$";
Regex regex = new Regex(pattern);
string correctColor = "#FFC0CBAA";
Match match = regex.Match(correctColor);
Console.WriteLine($"The color {correctColor} is {(match.Success ? "correct" : "wrong")}");
// Result: The color #FFC0CBAA is correct
The regex pattern for the HH:MM 24-hour format
To validate day time with the HH:MM 24-hour format with optional leading 0, use the regex pattern:
“^(\d|0\d|1\d|2[0-3]):[0-5]\d$
”
using System.Text.RegularExpressions;
string pattern = @"^(\d|0\d|1\d|2[0-3]):[0-5]\d$";
Regex regex = new Regex(pattern);
string time = "14:18";
Match match = regex.Match(time);
Console.WriteLine($"The day time {time} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for the HH:MM 12-hour format
To validate day time with the HH:MM 12-hour format with optional leading 0 and mandatory meridiem (AM/PM), use the regex pattern: "^((1[0-2]|0?[1-9]):([0-5]\d) ?([AaPp][Mm]))$"
using System.Text.RegularExpressions;
string pattern = @"^((1[0-2]|0?[1-9]):([0-5]\d) ?([AaPp][Mm]))$";
Regex regex = new Regex(pattern);
string time = "11:18am";
Match match = regex.Match(time);
Console.WriteLine($"The day time {time} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for email
To validate an email string, use the regex pattern: "^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|((\d{1,3}\.){3}\d{1,3}))$".
using System.Text.RegularExpressions;
string pattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
+ "@"
+ @"((([\-\w]+\.)+[a-zA-Z]{2,4})|((\d{1,3}\.){3}\d{1,3}))$";
Regex regex = new Regex(pattern);
string email = "test.email.and+symbol@domain.com";
Match match = regex.Match(email);
Console.WriteLine($"The email {email} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for phone number
To validate a phone number string (international or local, with round brackets or without), use the regex pattern: "^[+]*(\d{1}|\(\d{1}\)){0,1}[-\s\./]{0,1}(\d{3}|\(\d{3}\)){0,1}[-\s\./]{0,1}[-\s\./\d]{2,8}$"
.
using System.Text.RegularExpressions;
string pattern = @"^[+]*(\d{1}|\(\d{1}\)){0,1}[-\s\./]{0,1}(\d{3}|\(\d{3}\)){0,1}[-\s\./]{0,1}[-\s\./\d]{2,8}$";
Regex regex = new Regex(pattern);
string phone = "+(1) 425 555-0100";
Match match = regex.Match(phone);
Console.WriteLine($"The phone number {phone} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for ZIP code
To validate a ZIP code (USA and Canada, 5-digit, 4-digit (many countries)), use the regex pattern:
"^((\d{5}-?\d{4})|(\d{4,5})|([A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d))$"
.
using System.Text.RegularExpressions;
string pattern = @"^((\d{5}-?\d{4})|(\d{4,5})|([A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d))$";
Regex regex = new Regex(pattern);
string zipcode = "90210-1234";
Match match = regex.Match(zipcode);
Console.WriteLine($"The ZIP code {zipcode} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for strong password
To validate a strong password string (validation conditions: minimum 1 special character, optional containing space, minimum 1 lower case, minimum 1 upper case, minimum 8 characters, minimum 2 digits), use the regex pattern: "^(?=(.*\d){2})(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z\d]).{8,}$"
.
using System.Text.RegularExpressions;
string pattern = @"^(?=(.*\d){2})(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z\d]).{8,}$";
Regex regex = new Regex(pattern);
string password = "!Lov34MyPiano";
Match match = regex.Match(password);
Console.WriteLine($"The password {password} is {(match.Success ? "strong" : "weak")}");
The regex pattern for MD5 hash
To validate MD5 hash (password or key), use regex pattern: "^[A-Fa-f0-9]{32}$"
.
using System.Text.RegularExpressions;
string pattern = @"^[A-Fa-f0-9]{32}$";
Regex regex = new Regex(pattern);
string md5hash = "00236a2ae558018ed13b5222ef1bd987";
Match match = regex.Match(md5hash);
Console.WriteLine($"The md5 hash {md5hash} is {(match.Success ? "correct" : "wrong")}");
The regex pattern for latitude and longitude pairs
To validate a string containing a latitude and longitude pair (for validating GPS coordinates, searching Google maps, geocoding, etc.), use the regex pattern:
"^((\-?([1-8])?\d(\.\d{0,9})?)|(\-?90(\.0+)?))(\,\ +)((\-?((1[0-7])|\d)?\d(\.\d{0,9})?)|(\-?180(\.0+)?))$"
.
using System.Text.RegularExpressions;
string pattern = @"^((\-?([1-8])?\d(\.\d{0,9})?)|(\-?90(\.0+)?))(\,\ +)((\-?((1[0-7])|\d)?\d(\.\d{0,9})?)|(\-?180(\.0+)?))$";
Regex regex = new Regex(pattern);
string latlng = "42.455541, -174.587429";
Match match = regex.Match(latlng);
Console.WriteLine($"The latitude and longitude pair {latlng} is {(match.Success ? "correct" : "wrong")}");
Was this helpful?
1 / 0