When checking if data entered by a user is in the correct format (e.g., zip codes, phone numbers, or email addresses), Regular Expressions are a very powerful tool.
In C#, you can use the Regex.IsMatch method from the System.Text.RegularExpressions namespace to easily determine whether a target string matches a specific pattern, returning the result as true or false.
Table of Contents
- Implementation of Postal Code Format Check
- Sample Code
- Execution Result
- Explanation and Technical Points
- Regex.IsMatch Method
- Verbatim String Literal (@)
- Importance of Start (^) and End ($) Anchors
Implementation of Postal Code Format Check
In the following sample code, I determine whether a string completely matches the Japanese postal code format (3 digits – hyphen – 4 digits).
Sample Code
using System;
using System.Text.RegularExpressions; // Required to use regular expression features
public class Program
{
public static void Main()
{
// List of strings to check
string[] inputs = new string[]
{
"100-0001", // Correct format
"1234567", // Missing hyphen
"123-45ab", // Contains non-numeric characters
"99-99999", // Incorrect number of digits
"text 123-4567 end" // Contains the format, but has extra characters
};
// Regular Expression Pattern:
// ^ : Start of line
// \d{3} : 3 digits
// - : Hyphen
// \d{4} : 4 digits
// $ : End of line
string pattern = @"^\d{3}-\d{4}$";
Console.WriteLine($"Pattern: {pattern}\n");
foreach (var text in inputs)
{
// Regex.IsMatch(target string, pattern)
// Returns true if matched, false otherwise
bool isMatch = Regex.IsMatch(text, pattern);
string result = isMatch ? "O Matched" : "X Not Matched";
Console.WriteLine($"{text,-15} => {result}");
}
}
}
Execution Result
Pattern: ^\d{3}-\d{4}$
100-0001 => O Matched
1234567 => X Not Matched
123-45ab => X Not Matched
99-99999 => X Not Matched
text 123-4567 end => X Not Matched
Explanation and Technical Points
1. Regex.IsMatch Method
The static method Regex.IsMatch(input, pattern) determines whether the string in the first argument matches the regular expression pattern in the second argument. It returns a bool.
Note: If you are using the same pattern repeatedly in a loop, creating a Regex instance can be more efficient, but for single checks, this static method is the easiest way.
2. Verbatim String Literal (@)
When defining a regular expression pattern as a string, it is recommended to add @ at the beginning (@"...").
In standard strings, the backslash \ is treated as an escape character. Therefore, to write \d (digit) for a regular expression, you would normally have to double it as \\d. By using @, you can write \d directly, which improves readability.
3. Importance of Start (^) and End ($) Anchors
When performing validation (input checking), it is very important to include ^ (representing the start of the line) and $ (representing the end of the line) in your pattern.
^\d{3}-\d{4}$: Matches only if the string is exactly “123-4567”.\d{3}-\d{4}(Without anchors): This will returntrueif the pattern exists anywhere in the string. For example, the sentence “My address is 100-0001” would be considered a match, which is usually not what you want for strict validation.
