QT利用lamda正则表达式取出字符串中的浮点数与整数
生活随笔
收集整理的這篇文章主要介紹了
QT利用lamda正则表达式取出字符串中的浮点数与整数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、待取的數據格式
- 二、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.讀取到的數據
這里是一行一行讀取時候輸出的結果(這一行語句的輸出):
以下為使用正則表達式匹配的輸出。
("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正则表达式取出字符串中的浮点数与整数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow2.0的cpu与gp
- 下一篇: TensorFlow官方入门实操课程-一