Question

I'm trying to get Qt to match a MAC Address ( 1a:2b:3c:4d:5e:6f ) using a QRegExp. I can't seem to get it to match - what am I doing wrong?

I am forcing it to try and match the string:

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

Here are my attempts:

// 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 ) {
Was it helpful?

Solution

In your first example opening bracket is missing and \. is incorrect (read help for explanations), in both a-F matches nothing, due to 'a' > 'F'.

The correct answer you can find in the comment of kenrogers, but I'll duplicate it for you:

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

If you want to match . you should use:

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

If you also want to match lower case characters, you should use:

([0-9A-Fa-f]{2}[:-\\.]){5}([0-9A-Fa-f]{2})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top