Different MAC Addresses Regex

后端 未结 7 787
旧时难觅i
旧时难觅i 2021-01-06 08:30

What is the correct regular expression for matching MAC addresses ? I googled about that but, most of questions and answers are incomplete. They only provide a regular expre

7条回答
  •  执念已碎
    2021-01-06 09:01

    Why so much code? Here is how you can "normalize" your mac address:

    mac.replaceAll("[^a-fA-F0-9]", "");

    And here is a way to validate it:

    public boolean validate(String mac) {
       Pattern p = Pattern.compile("^([a-fA-F0-9][:-]){5}[a-fA-F0-9][:-]$");
       Matcher m = p.matcher(mac);
       return m.find();
    }
    

提交回复
热议问题