2013-05-08 53 views
0

我試圖讓Qt使用QRegExp匹配MAC地址(1a:2b:3c:4d:5e:6f)。我似乎無法讓它匹配 - 我做錯了什麼?Qt 4.8.4 MAC地址QRegExp

我迫使它試圖匹配字符串:

"48:C1:AC:55:86:F3" 

這裏是我的嘗試:

// Define a RegEx to match the mac address 
//QRegExp regExMacAddress("[0-9a-F]{1,2}[\.:-]){5}([0-9a-F]{1,2}"); 

//QRegExp regExMacAddress("[0-9a-F]{0,2}:[0-9a-F]{0,2}:[0-9a-F]{0,2}:[0-9a-F]{0,2}:[0-9a-F]{0,2}:[0-9a-F]{0,2}"); 

//regExMacAddress.setPatternSyntax(QRegExp::RegExp); 

// Ensure that the hexadecimal characters are upper case 
hwAddress = hwAddress.toUpper(); 

qDebug() << "STRING TO MATCH: " << hwAddress << "MATCHED IT: " << regExMacAddress.indexIn(hwAddress) << " Exact Match: " << regExMacAddress.exactMatch(hwAddress); 

// Check the mac address format 
if (regExMacAddress.indexIn(hwAddress) == -1) { 
+0

你試過嗎? http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address – kenrogers 2013-05-08 13:50:48

+0

是的 - 這些解決方案不適用於我的Qt。 – PhilBot 2013-05-08 14:04:37

+0

你還沒有顯示你餵它的數據是什麼樣的。 – 2013-05-08 14:07:08

回答

1

在你的第一個例子中左括號缺失和\.是不正確的(讀help爲解釋),在a-F都沒有匹配任何內容,原因是'a' > 'F'

正確的答案,你可以在kenrogers的評論發現,但我會複製它給你:

([0-9A-F]{2}[:-]){5}([0-9A-F]{2}) 

如果你想匹配.你應該使用:

([0-9A-F]{2}[:-\\.]){5}([0-9A-F]{2}) 

如果你也想匹配小寫字母,你應該使用:

([0-9A-Fa-f]{2}[:-\\.]){5}([0-9A-Fa-f]{2}) 
+0

謝謝您匹配 - >([0-9A-Fa-f] {2} [: - \\。]){5}([0-9A-Fa-f] {2}) – PhilBot 2013-05-08 19:47:27