QT中使用正则表达式的方法
使用的类:QRegExp
QString str = "offsets: 1.23 .50 71.00 6.00";
QRegExp rx("\\d*\\.\\d+"); // primitive floating point matching
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
++count;
pos += rx.matchedLength();
}
// pos will be 9, 14, 18 and finally 24; count will end up as 4
QRegExp rxlen("(\\d+)(?:\\s*)(cm|inch)");
int pos = rxlen.indexIn("Length: 189cm");
if (pos > -1) {
QString value = rxlen.cap(1); // "189"
QString unit = rxlen.cap(2); // "cm"
// ...
}
1、解析MAC地址
地址类型 12-23-34-45-56-67
使用的表达式(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)
QString str="12-34-56-78-90";
QRegExp rx("([0-9A-Za-z]+)-([0-9A-Za-z]+)-([0-9A-Za-z]+)-([0-9A-Za-z]+)-([0-9A-Za-z]+)-([0-9A-Za-z]+)");
rx.indexIn(str);
for(int i=0; i<6; i++)
qDebug()<<rx.cap(i);