Skip to content

Instantly share code, notes, and snippets.

@lidio601
Created March 2, 2020 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidio601/d5813c467db6df68e6366f8ecc04cebf to your computer and use it in GitHub Desktop.
Save lidio601/d5813c467db6df68e6366f8ecc04cebf to your computer and use it in GitHub Desktop.
simple function to normalize mac addresses
final d = "([0-9A-F]{2})";
final s = "[:\.-]?";
final RegExp re = RegExp(
"$d$s$d$s$d$s$d$s$d$s$d",
multiLine: false,
);
/// MAC addresses will come in the following shape or form:
/// 00-11-22-33-44-55
/// 00:11:22:33:44:55
/// 00.11.22.33.44.55
/// 001122334455
/// 0011.2233.4455
String normalizeMacAddress(String macaddress) {
final matches = re.allMatches(macaddress);
if (matches.length < 1) {
return null;
}
final match = matches.first;
return "${match.group(1)}:${match.group(2)}:${match.group(3)}:${match.group(4)}:${match.group(5)}:${match.group(6)}";
}
void test(address) {
final result = normalizeMacAddress(address);
print("$address => $result");
}
void main() {
test("00-11-22-33-44-55");
test("00:11:22:33:44:55");
test("00.11.22.33.44.55");
test("001122334455");
test("0011.2233.4455");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment