Created
March 2, 2020 05:47
-
-
Save lidio601/d5813c467db6df68e6366f8ecc04cebf to your computer and use it in GitHub Desktop.
simple function to normalize mac addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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