2013-05-30 68 views
2

匹配MAC地址的正確正則表達式是什麼?我google了一下,但大部分questions and answers是不完整的。他們只爲the standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits, separated by hyphens - or colons :.提供正則表達式。但是,這不是現實世界的情況。許多路由器,交換機等網絡設備供應商提供的格式,如MAC地址:不同的MAC地址正則表達式

3D:F2:C9:A6:B3:4F //<-- standard 
3D-F2-C9-A6-B3:4F //<-- standard 
3DF:2C9:A6B:34F 
3DF-2C9-A6B-34F 
3D.F2.C9.A6.B3.4F 
3df2c9a6b34f // <-- normalized 

我有什麼,直到這一刻是這樣的:

public class MacAddressFormat implements StringFormat { 
    @Override 
    public String format(String mac) throws MacFormatException { 
    validate(mac); 
    return normalize(mac); 
    } 

    private String normalize(String mac) { 

    return mac.replaceAll("(\\.|\\,|\\:|\\-)", ""); 
    } 

    private void validate(String mac) { 


    if (mac == null) { 
     throw new MacFormatException("Empty MAC Address: !"); 
    } 
    // How to combine these two regex together ? 
    //this one 
    Pattern pattern = Pattern.compile("^([0-9A-Fa-f]{2}[\\.:-]){5}([0-9A-Fa-f]{2})$"); 
    Matcher matcher = pattern.matcher(mac); 
    // and this one ? 
    Pattern normalizedPattern = Pattern.compile("^[0-9a-fA-F]{12}$"); 
    Matcher normalizedMatcher = normalizedPattern.matcher(mac); 

    if (!matcher.matches() && !normalizedMatcher.matches()) { 
     throw new MacFormatException("Invalid MAC address format: " + mac); 
    } 

    } 
} 

如何喲將二者結合起來的正則表達式中的代碼?

+2

爲什麼不去掉所有非十六進制小寫的所有十六進制然後比較?然後,在 – Patashu

+0

Yup中檢查是否匹配12個十六進制字符(標準化模式),然後檢查它是什麼格式。 – Aquillo

回答

5

爲什麼這麼多的代碼? 這裏是你如何可以 「正常化」 你的mac地址:

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

這裏是一個方法來驗證它:

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(); 
} 
+0

您能否提供與上述正則表達式匹配的有效MAC地址示例?圖9c:8E:99:F3:BD:1B不匹配 – Adelin

+0

@Test 公共無效regexTest(){ 字符串MAC = 「9C:8E:99:F3:BD:1b」 時; 字符串替換= mac.replaceAll( 「[^ A-FA-F0-9]」, 「」); System.out.println(替換); 模式圖案= Pattern.compile( 「^([A-FA-F0-9] [: - ]){5} [A-FA-F0-9] [: - ] $」); Matcher matcher = pattern.matcher(替換); System.out.println(matcher.matches()); } 輸出爲: 9c8e99f3bd1b 假 – Adelin

+2

我不太獲取正則表達式。它是否使m.find()與整個MAC匹配?並且在那裏處理帶有「{5}」的'3DF:2C9:A6B:34F'?我覺得你需要5對[a-fA-F0-9]和[: - ],例如'5:'來匹配它。很顯然,我需要閱讀我的「模式」。 :-) 規範和使用'^ [A-FA-F0-9] {12} $'將是我的做法。 – Qben

0

基於AlexR的回答是:

private static Pattern patternMacPairs = Pattern.compile("^([a-fA-F0-9]{2}[:\\.-]?){5}[a-fA-F0-9]{2}$"); 
private static Pattern patternMacTriples = Pattern.compile("^([a-fA-F0-9]{3}[:\\.-]?){3}[a-fA-F0-9]{3}$"); 

private static boolean isValidMacAddress(String mac) 
{ 
    // Mac addresses usually are 6 * 2 hex nibbles separated by colons, 
    // but apparently it is legal to have 4 * 3 hex nibbles as well, 
    // and the separators can be any of : or - or . or nothing. 
    return (patternMacPairs.matcher(mac).find() || patternMacTriples.matcher(mac).find()); 
} 

這並不完美,因爲它會匹配AB:CD.EF-123456之類的東西。如果你想避免這種情況,你可以比我更聰明地使它匹配每個地方的相同字符,或者只是將兩個模式分成一個用於每個可能的分隔符。 (?共6個)

0

我知道這個問題是慈祥的老人,但我認爲不是問題的一個很好的解決方案:

兩個步驟做

  1. 更改MAC字符串解析到您選擇的格式(例如XX:XX:XX:XX:XX:XX)
  2. 收件這驗證與所述格式的字符串上面

的方法

實施例:

1)

可以很容易地通過調用

public static String parseMac(String mac) { 
    mac = mac.replaceAll("[:.-]", ""); 

    String res = ""; 
    for (int i = 0; i < 6; i++) { 
     if (i != 5) { 
      res += mac.substring(i * 2, (i + 1) * 2) + ":"; 
     } else { 
      res += mac.substring(i * 2); 
     } 
    } 
    return res; 
} 

實現這一點:.-是將從字符串

2)

被去除的字符
public static boolean isMacValid(String mac) { 
    Pattern p = Pattern.compile("^([a-fA-F0-9]{2}[:-]){5}[a-fA-F0-9]{2}$"); 
    Matcher m = p.matcher(mac); 
    return m.find(); 
} 

Ofc你可以使模式成爲本地cl驢變量來提高你的代碼的效率。

0

嘗試此,工作完美..

MAC地址是由製造商標識分配給最網絡適配器或網絡接口卡(NIC)的唯一識別符,IEEE 802標準使用48個叮咬或6個字節以代表一個MAC地址。這種格式給出了281,474,976,710,656個可能的唯一MAC地址。

IEEE 802個標準定義3種常用的格式打印在十六進制數字MAC地址:

六組由連字符分隔兩個十六進制數字( - ),如01-23-45-67-89-AB

六組的兩個十六進制數字用冒號(:),像01分離:23:45:67:89:AB

三組由點分隔的四個十六進制數字(),等0123.4567.89ab

public boolean macValidate(String mac) { 
     Pattern p = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"); 
     Matcher m = p.matcher(mac); 
     return m.find(); 
}