文章目录

  • 前言
  • 一、待取的数据格式
  • 二、Qt使用正则表达式
    • 1.加入头文件
    • 2.cpp中代码
    • 2.读取到的数据
  • 总结

前言

利用正则表达式实现取出一串字符串中的浮点数与正整数


一、待取的数据格式

以下为待匹配的原始数据

<auvroute id="oligei" loop="loop" count="23"><routepoint index="1" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="2" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="3" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="4" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="5" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="6" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="7" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="8" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="9" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="10" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="11" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="12" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="13" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="14" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="15" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="16" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/><routepoint index="17" latitude="14.4449" longitude="117.053" Depth="100.0" Section_Sum="10" Point_CtrL="27" Main_Propulsion_rpm="0.0" SampleTime="50.0" Task_Ctrl="0"/></auvroute>

二、Qt使用正则表达式

1.加入头文件

代码如下:

#include <QFile>
#include <QDebug>

2.cpp中代码

    QString displayString;QFile file("E:/QTCode/QTlamda/wpt_test.WPT");  //打开文件if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){qDebug()<<"Can't open the file!"<<endl;}while(!file.atEnd())  //一行一行读取直到结束{QByteArray line = file.readLine();QString str(line);qDebug()<< str;displayString.append(str);}qDebug()<<displayString;//    QString pattern("\\d{1,5}");//任意一个数字,0~9 中的任意一个 正则表达式提取“[XXX]”,XXX为任意字符串。QString pattern("\\d+(\\.\\d+)|-?\\d+");   //匹配字符串中所有的浮点数以及整数
//      QString pattern("^(\\d){0,3}(\\.\\d{0,2})?$");QRegExp rx(pattern);rx.setMinimal(false);//--1 提取字符串QStringList list;int pos = 0;while ((pos = rx.indexIn(displayString, pos)) != -1){list.append(rx.cap(0)); //把提取到的字符串保存下来pos += rx.matchedLength();}qDebug()<<list;

2.读取到的数据

这里是一行一行读取时候输出的结果(这一行语句的输出):

"<auvroute id=\"oligei\" loop=\"loop\" count=\"23\">\n"
"<routepoint index=\"1\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"2\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"3\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"4\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"5\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"6\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"7\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"8\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"9\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"10\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"11\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"12\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"13\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"14\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"15\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"16\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"17\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"18\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"19\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"20\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"21\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"22\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"<routepoint index=\"23\" latitude=\"14.4449\" longitude=\"117.053\" Depth=\"100.0\" Section_Sum=\"10\" Point_CtrL=\"27\" Main_Propulsion_rpm=\"0.0\" SampleTime=\"50.0\" Task_Ctrl=\"0\"/>\r\n"
"</auvroute>"

以下为使用正则表达式匹配的输出。

("23", "1", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "2", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "3", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "4", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "5", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "6", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "7", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "8", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "9", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "10", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "11", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "12", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "13", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "14", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "15", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "16", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "17", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "18", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "19", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "20", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "21", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "22", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0", "23", "14.4449", "117.053", "100.0", "10", "27", "0.0", "50.0", "0")

总结

这里只是简单实现了正则表达式的应用,具体使用还是得看一下正则表达式的原理。
可以在这里在线测试自己的正则表达式
正则表达式在线测试

QT利用lamda正则表达式取出字符串中的浮点数与整数相关推荐

  1. qt中利用正则表达式提取字符串中的浮点数和整数

    当我们需要从一个字符串中提取数字时,可以用正则表达式来操作 QString str = "你的金额是: 0.22示范33^%zd卡兹克44是13.5x的5&ss"; QRe ...

  2. java 去除字符串中的英文_Java利用正则表达式去掉字符串中的英文

    利用正则表达式去掉字符串中的英文String str = "111,aaa,222,bbb"; Pattern p = Pattern.compile("[a-zA-z] ...

  3. python正则表达式提取数字比较好_python正则表达式从字符串中提取数字的思路详解...

    python从字符串中提取数字 使用正则表达式,用法如下: ## 总结 ## ^ 匹配字符串的开始. ## $ 匹配字符串的结尾. ## \b 匹配一个单词的边界. ## \d 匹配任意数字. ## ...

  4. java取出字符串中的后四位_[原]Java面试题-将字符串中数字提取出来排序后输出...

    [Title][原]Java面试题-将字符串中数字提取出来排序后输出 [Date]2013-09-15 [Abstract]很简单的面试题,要求现场在纸上写出来. [Keywords]面试.Java. ...

  5. php中怎么截取字符串最后一个字符,php如何取出字符串中的最后几个字符

    php取出字符串中的最后几个字符的方法:可以利用substr()函数来实现.substr()函数可以返回字符串的提取部分,如果失败则返回FALSE,或者返回一个空字符串,如:[substr(" ...

  6. python正则取字符串日期_python 正则表达式获取字符串中所有的日期和时间

    提取日期前的处理 1.处理文本数据的日期格式统一化 text = "2015年8月31日,衢州元立金属制品有限公司仓储公司(以下简称元立仓储公司)成品仓库发生一起物体打击事故,造成直接经济损 ...

  7. python使用正则表达式统计字符串中出现次数最多的数字

    python使用正则表达式统计字符串中出现次数最多的数字 #python使用正则表达式统计字符串中出现次数最多的数字 # find the most occurring element import ...

  8. python使用正则表达式删除字符串中的其它字符只保留数字和字母

    python使用正则表达式删除字符串中的其它字符只保留数字和字母 #python使用正则表达式删除字符串中的其它字符只保留数字和字母 # Python code to demonstrate # to ...

  9. python使用正则表达式抽取字符串中最大数值数字

    python使用正则表达式抽取字符串中最大数值数字 #python使用正则表达式抽取字符串中最大数值数字 # Function to extract maximum numeric value fro ...

最新文章

  1. 前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
  2. Python解析命令行读取参数 -- argparse模块
  3. oracle正则判断身份证号,Oracle中查询使用正则表达式函数REGEXP
  4. 列表表格以及媒体元素
  5. 2022,普平数据中心招聘来啦!
  6. 苹果计算机咋出记录,怎么查看Mac电脑的开机记录?
  7. 一、认识Python
  8. 微型计算机的分类有,微型计算机的分类
  9. excel打印预览在哪里_Excel如何打印表格,每页纸都有标题?
  10. react本地储存_如何使用React和本地存储构建freeCodeCamp的配方框
  11. (转)C++的 RTTI 概念和用途
  12. 两个链表求交集_实现两个排序链表的并集和交集
  13. vue复选框CheckBox清空选中的值
  14. (四)Go 语言编译流程简述
  15. SQL查询语句 select
  16. xposed 主动调用方法_操作方法:主动容量管理
  17. 计算文件的hash值方法 | 使用powershell 以及 使用python
  18. 在线pdf转word
  19. 基于Arduino的智能环境监测与反馈系统
  20. 矩阵键盘控制拉闭幕式流水灯

热门文章

  1. 《图解HTTP》读书笔记--第4章返回结果的HTTP状态码
  2. 多重背包2[二进制位优化]
  3. C++归并排序递归写法
  4. python定义字符串1hdhdjdjd_python基础总结(字符串)
  5. mysql 代理作业_查看SQLServer 代理作业的历史信息
  6. leetcode 450. 删除二叉搜索树中的节点 c语言实现
  7. golang sync.Map 使用
  8. python宝石与石头_771. 宝石与石头
  9. 关闭oracle自动统计,禁用oracle 11g 的统计数据自动功能
  10. flutter 局部状态和全局状态区别_Flutter状态管理