0

我试图让 Qt 使用 QRegExp 匹配 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 ) {
4

1 回答 1

1

在您的第一个示例中,左括号丢失且不\.正确(请阅读帮助以获取解释)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})
于 2013-05-08T18:29:35.937 回答