在正则表达式里面先找出fifo文件名和路径,然后找出端口号:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include<regex>
#include <fstream>
//int mkfifo( const char* path,
// mode_t mode );
using namespace std;
#include <string>
void test()
{
std::regex reg("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))\\sMbits");
std::regex reg1("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
std::string str = "这是中文示例abc 1234.2 a 匹配浮点数值XX5678 2.6 5.789 -0.23411 +01.45";
string row = "[ 5] 38.0-39.0 sec 11.5 MBytes 96.4 Mbits/sec 0.294 ms 11/ 8208 (0.13%)";
const std::sregex_iterator end;
for (std::sregex_iterator iter(std::cbegin(str), std::cend(str), reg1); iter != end;++iter)
std::cout << iter->str() << std::endl;
for (std::sregex_iterator iter(std::cbegin(row), std::cend(row), reg); iter != end;++iter)
std::cout << iter->str() << std::endl;
}
void test1(string str)
{
//string str = "Hello 2018, Bye 2017";
//string str = "[ 5] 38.0-39.0 sec 11.5 MBytes 96.4 Mbits/sec 0.294 ms 11/ 8208 (0.13%)";
smatch result;
regex Bandwidth("-?(([0-9]\\d*\\.[1-9]\\d*)|(0)|(0\\.[1-9]\\d*)|([0-9]\\d*))\\sMbits");//(".+(\\d Mbits/sec).+(\\(\\d%\\))"); //
//regex Packageloss("\\(([0-9]+%)\\)");
regex Packageloss("\\(-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([0-9]\\d*))\\%\\)");
//regex Value("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
//迭代器声明
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp,value;
cout << "==============================" << endl;
while (regex_search(iterStart, iterEnd, result, Bandwidth))
{
temp = result[0];
cout << "==== bandwidth: " << result[1] << endl;
iterStart = result[0].second; //更新搜索起始位置,搜索剩下的字符串
}
cout << "----------------------------------" << endl;
while (regex_search(iterStart, iterEnd, result, Packageloss))
{
temp = result[0];
cout << "==== package loss: " << result[1] << endl;
iterStart = result[0].second; //更新搜索起始位置,搜索剩下的字符串
}
cout << "-------+++------------------------" << endl;
}
int main(void)
{
string line;
//打开文件data.txt
ifstream fin("ethernetTest.sh");
string s;
test();
int ret,fd,pid;
//regex Bandwidth("\\s\\s([1-9,\\\\.]+)\\sMbits");
regex Bandwidth(".+(\\d Mbits/sec).+(\\(\\d%\\))");
cout << "\\s\\s([1-9,\\\\.]+)\\sMbits" << endl;
//"MBytes .* Mbits/sec"
regex Packageloss("\\(([0-9]+%)\\)");
regex filePath("/var/fifo_([0-9]\\d*)");
string::const_iterator itStart;// = fp.begin();
string::const_iterator itEnd;// = fp.end();
cout << "==============================" << endl;
while( getline(fin,s) ){
itStart = s.begin();
itEnd = s.end();
smatch result;
if (regex_search(itStart, itEnd, result, filePath)) {
cout << "+++++ file path: " << result[0] << " num: " << result[1] << endl;
} else {
cout << "--- no search ---" << endl;
}
}
ret = mkfifo("/var/fifo1",S_IRUSR | S_IWUSR);
cout << "ret: " << ret << endl;
pid = fork();
if (pid > 0) {
cout << "- parent -" << endl;
fd = open("/var/fifo1",O_RDONLY,0);
if(fd < 0){
cout << "open error!\n" ;
return -1;
}
char buf1[500];
while(1) {
cout << "please waiting...\n";
ssize_t s = read(fd,buf1,sizeof(buf1)-1);
if(s > 0) {
buf1[s-1] = 0;
string row = buf1;//"[ 5] 38.0-39.0 sec 11.5 MBytes 96.4 Mbits/sec 0.294 ms 11/ 8208 (0.13%)";//buf1;
cout << row << endl;
test1(row);
} else if (s == 0) {
cout << "client quit,exit ..." << endl;
break;
}
}
} else if (pid == 0){
cout << "- child -" << endl;
fd = open("/var/fifo1",O_WRONLY,0);
if(fd < 0){
cout << "open error!\n" ;
return -1;
}
char buf1[500];
while(1) {
cout << "client\n";
fflush(stdout);
ssize_t s = read(0,buf1,sizeof(buf1)-1);
if(s > 0) {
buf1[s] = 0;
write(fd,buf1,strlen(buf1));
}
}
} else {
cout << "fork error!\n" ;
}
cout << "file is exit!\n" ;
close(ret);
}
运行结果:
# ./fifo_test
1234.2
2.6
5.789
-0.23411
1.45
96.4 Mbits
\s\s([1-9,\\.]+)\sMbits
==============================
--- no search ---
--- no search ---
--- no search ---
--- no search ---
--- no search ---
+++++ file path: /var/fifo_1200 num: 1200
+++++ file path: /var/fifo_1201 num: 1201
+++++ file path: /var/fifo_1202 num: 1202
+++++ file path: /var/fifo_1203 num: 1203
+++++ file path: /var/fifo_1205 num: 1205
--- no search ---
+++++ file path: /var/fifo_1206 num: 1206
+++++ file path: /var/fifo_1207 num: 1207
+++++ file path: /var/fifo_1209 num: 1209
+++++ file path: /var/fifo_1210 num: 1210
--- no search ---
--- no search ---
--- no search ---
--- no search ---
--- no search ---
ret: -1
- parent -
- child -
please waiting...
client
|