String - IsMatch
Indicates whether the specified regular expression finds a match in the specified input string.
public static void Main() { string[] partNumbers= { "1298-673-4192", "A08Z-931-468A", "_A90-123-129X", "12345-KKA-1230", "0919-2893-1256" }; string pattern = @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$"; foreach (string partNumber in partNumbers) { // C# Extension Method: String - IsMatch var result = partNumber.IsMatch(pattern); Console.WriteLine("{0} {1} a valid part number.", partNumber, result ? "is" : "is not"); } }
View Source
using System; using System.Text.RegularExpressions; public static partial class Extensions { /// <summary> /// Indicates whether the specified regular expression finds a match in the specified input string. /// </summary> /// <param name="input">The string to search for a match.</param> /// <param name="pattern">The regular expression pattern to match.</param> /// <returns>true if the regular expression finds a match; otherwise, false.</returns> public static Boolean IsMatch(this String input, String pattern) { return Regex.IsMatch(input, pattern); } /// <summary> /// Indicates whether the specified regular expression finds a match in the specified input string, using the /// specified matching options. /// </summary> /// <param name="input">The string to search for a match.</param> /// <param name="pattern">The regular expression pattern to match.</param> /// <param name="options">A bitwise combination of the enumeration values that provide options for matching.</param> /// <returns>true if the regular expression finds a match; otherwise, false.</returns> public static Boolean IsMatch(this String input, String pattern, RegexOptions options) { return Regex.IsMatch(input, pattern, options); } }