我正在尝试使用QRegExp让Qt匹配MAC地址( 1a:2b:3c:4d:5e:6f )。我似乎不能让它匹配-我做错了什么?
我强制它尝试匹配字符串:
"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 ) {发布于 2013-05-09 02:29:36
在您的第一个示例中,由于'a' > 'F',两个a-F都不匹配,缺少左方括号并且\.不正确(请阅读help以获得解释)。
你可以在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})https://stackoverflow.com/questions/16442318
复制相似问题