实例1 sql语句相关
{
/**
* \s 表示一个\t、一个\n等
* \s Matches a whitespace character (QChar::isSpace()).
* \s* 多个
* \s+ 至少一个
* \s{2} 两个
* \s{1,2} 一个到两个
* \w 表示一个字母、一个数字、一个汉字等
* \w Matches a word character (QChar::isLetterOrNumber(), QChar::isMark(), or '_').
* \w* 多个
* \w+ 至少一个
* \w{2} 两个
* \w{1,2} 一个到两个
* () 表示提取的位置
*/
QString Output = "select obid from cey_bt_table where ch_billno = 'Q001' ";
QRegExp rx(R"(select\s*(\w*)\s*from\s*(\w*)\s*where\s*(\w*)\s*=\s*'(\w*)'\s*)");
rx.indexIn(Output);
QStringList qsl = rx.capturedTexts();
for(int i=0; i<qsl.count(); i++)
{
qDebug()<<"Da Data Thing: "<< i << " Da Value Thing: " << qsl.at(i);
}
return 1;
}
运行结果:
Da Data Thing: 0 Da Value Thing: "select obid from cey_bt_table where ch_billno = 'Q001' " Da Data Thing: 1 Da Value Thing: "obid" Da Data Thing: 2 Da Value Thing: "cey_bt_table" Da Data Thing: 3 Da Value Thing: "ch_billno" Da Data Thing: 4 Da Value Thing: "Q001" |
实例2 mac地址相关
{
/**
* \d 表示一个数字
* \d Matches a digit (QChar::isDigit()).
* \d+ 表示重复一次或者多次 1、12、23
* \d* 表示重复零次或者多次 _、12、11
* \d{2} 表示只能匹配两个字符
*/
QString Output;
Output="Duration: 00:01:25.65, start: 0.050000, bitrate: 5200 kb/s";
QRegExp rx = QRegExp(R"(Duration:\s*(\d{2}:\d{2}:\d{2}.\d{2}),\s*start:\s*(\d+\.\d+),\s*bitrate:\s*(\d*)\s*kb/s)");
rx.indexIn(Output);
QStringList qsl = rx.capturedTexts();
for(int i=0; i<qsl.count(); i++)
{
qDebug()<<"Da Data Thing: "<< i << " Da Value Thing: " << qsl.at(i);
}
}
运行结果:
Da Data Thing: 0 Da Value Thing: "Duration: 00:01:25.65, start: 0.050000, bitrate: 5200 kb/s" Da Data Thing: 1 Da Value Thing: "00:01:25.65" Da Data Thing: 2 Da Value Thing: "0.050000" Da Data Thing: 3 Da Value Thing: "5200" |