2013-05-08 57 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