Qt 编程:使用正则表达式截取字符串中的小数
在 Qt 中,您可以使用 QString 类的一些方法来截取字符串。以下是一个使用正则表达式的示例代码,可以截取出字符串 '0.001':
#include <QtDebug>
#include <QString>
#include <QRegularExpression>
int main()
{
QString str = 'PFB\n\r\n0.001 [deg]<F7>\r\n-->';
// 使用正则表达式匹配并截取字符串
QRegularExpression re('\\b\\d+\\.\\d+\\b');
QRegularExpressionMatch match = re.match(str);
if (match.hasMatch()) {
QString result = match.captured();
qDebug() << result;
}
return 0;
}
运行上述代码,输出结果为:
'0.001'
这里使用了正则表达式模式 \\b\\d+\\.\\d+\\b 来匹配小数。\\b 表示单词边界,\\d 表示数字,+ 表示匹配一个或多个,\\. 表示匹配小数点。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pi0Z 著作权归作者所有。请勿转载和采集!