C++ 简单的正则
本人初学C++ 如有遗漏和错误,望斧正!
本文只是简单的介绍一下C++对txt文件的简单读和写
希望大家喜欢
给个赞就最好了
(。・∀・)ノ
C++ 简单的正则
在Windows下读取txt文件时往往需要从中提取一些内容,比如GPS模块返回日志的txt文件中经纬度的提取
$GNRMC,071550.000,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,A*7F
因此为了方便我们可以利用简单的正则将其输出
GPS文件内容名称为GPSTest.txt
$GNRMC,071549.000,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*77
$GNRMC,071549.200,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*75
$GNRMC,071549.400,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*73
$GNRMC,071549.600,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*71
$GNRMC,071549.800,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7F
$GNRMC,071550.000,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7F
$GNRMC,071550.200,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7D
$GNRMC,071550.400,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7B
$GNRMC,071550.600,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*79
$GNRMC,071550.800,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*77
$GNRMC,071551.000,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7E
$GNRMC,071551.200,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7C
$GNRMC,071551.400,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7A
$GNRMC,071551.600,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*78
相关应用链接->GPS项目
1. 正则库
#include<regex>
其中拥有正则运算相关的函数
2. regex_match 演示
string str = "Hello_2018";
smatch result;//regex专用储存
regex pattern("(.{5})_(\\d{4})"); //匹配5个任意单字符 + 下划线 + 4个数字
if (regex_match(str, result, pattern))
{
cout << result[0] << endl; //完整匹配结果,Hello_2018
cout << result[1] << endl; //第一组匹配的数据,Hello
cout << result[2] << endl; //第二组匹配的数据,2018
cout<<"结果显示形式2"<<endl;
cout<< result.str() << endl; //完整结果,Hello_2018
cout<< result.str(1) << endl; //第一组匹配的数据,Hello
cout << result.str(2) << endl; //第二组匹配的数据,2018
}
//遍历结果
for (int i = 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
2. regex_search 演示
string str = "$GNRMC,071550.000,A,2325.0070,N,11637.7624,E,000.0,000.0,170920,,,A*7F";
smatch result;
regex pattern("\\d{4}.\\d{4}"); //匹配四个数字
//迭代器声明
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp;
while (regex_search(iterStart, iterEnd, result, pattern))
{
temp = result[0];
cout << temp << " ";
iterStart = result[0].second; //更新搜索起始位置,搜索剩下的字符串
}
输出结果:2018 2017
其中const_interator是函数迭代器->函数迭代器
3. regex_match 与 regex_search 的差别
cout << regex_match("123", regex("\\d")) << endl; //结果为0
cout << regex_search("123", regex("\\d")) << endl; //结果为1
4. match vs search
match()函数只检测RE是不是在string的开始位置匹配,
也就是说match()只有在0位置匹配成功的话才有返回,
如果不是开始位置匹配成功的话,match()就返回none.search()会扫描整个string查找匹配;
search()可以不从0位置开始匹配,这就是和match()的区别。
5.实践
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<regex>
#include<string>
using namespace std;
int main()
{
ifstream inFile; // 读写相关可见链接
inFile.open("GPSTest.txt");
if (!inFile.is_open())
{
cout << "Fail to open!"<< endl;
exit(EXIT_FAILURE);
}
string Inf;
regex pattern1("\\d{4}.\\d{4},\\w");
regex pattern2("\\d{5}.\\d{4},\\w");
smatch result1;
smatch result2;
while (getline(inFile, Inf))//getline(input,string(content)) 之所以不用 inFile.get(ch) 和 inFile.getline(ch,number)是因为其储存的是char和char数组
{
string::const_iterator interStart = Inf.begin();
string::const_iterator interEnd = Inf.end();
while (regex_search(interStart, interEnd,result1,pattern1))
{
while (regex_search(interStart, interEnd, result2, pattern2))
{
cout <<"经度:"<< result1[0] << '\t' << "纬度:"<< result2[0] << endl; // result为结果其中的编号为优先比配到的
interStart = interEnd; //*******函数迭代的头尾令其头部为空跳出这个while和下一个while循环
}
}
}
inFile.close();
system("pause");
return 0 ;
}
C++文件的简单读取
结果如图所示