REGEX for MAC Address Validation

I work with devices that have MAC Addresses all the time, and use this utility method frequently.

/// <summary>
/// Determines whether he specified macAddress is valid.
/// </summary>
/// <param name="macAddress">Mac Address.</param>
/// <returns>string MACAddress or null string if not valid</returns>
public static string IsValidMAC(string macAddress)
{
string result = "";
Regex rx = new Regex("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
Match m = rx.Match(macAddress);
result = m.Groups[0].Value;
if (result.Length == 17)
{
return result;
}
else
{
rx = new Regex("([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
Match m2 = rx.Match(macAddress);
result = m2.Groups[0].Value;
if (result.Length == 12)
{
return result;
}
return result;
}
}


Submission Date:  10/19/2005 7:52:59 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

By Peter Bromberg   Popularity  (807 Views)