I'd do:
if(expected.size()>2 || str.size()<4) return 0;
static QRegExp rx
("([A-Z][A-Z])#([0-9]*\\.?[0-9]+)");
if(!rx.exactMatch(cmd))
continue;
if(rx.cap(1)==expected)
return rx.cap(2).toFloat();
}
}
QString str
= "DV#1200.03,AB#34,SH#100\r\n\0";
float val = extractCmd(str, "SH");
float extractCmd(const QString &str, const QString &expected){
if(expected.size()>2 || str.size()<4) return 0;
QStringList commands = str.simplified().split(",");
static QRegExp rx("([A-Z][A-Z])#([0-9]*\\.?[0-9]+)");
foreach(QString cmd, commands){
if(!rx.exactMatch(cmd))
continue;
if(rx.cap(1)==expected)
return rx.cap(2).toFloat();
}
}
QString str = "DV#1200.03,AB#34,SH#100\r\n\0";
float val = extractCmd(str, "SH");
To copy to clipboard, switch view to plain text mode
Bookmarks