前言
利用正则表达式实现取出一串字符串中的浮点数与正整数
一、待取的数据格式
以下为待匹配的原始数据
<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")
总结
这里只是简单实现了正则表达式的应用,具体使用还是得看一下正则表达式的原理。
可以在这里在线测试自己的正则表达式
正则表达式在线测试