Qt 4.8.4 MAC地址QRegExp

时间:2013-05-08 13:42:42

标签: regex qt qt4 qregexp

我正在尝试使用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 ) {

1 个答案:

答案 0 :(得分: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})