C++ 确定字符串是否包含日期和时间
所以,我正在做一个项目,我找到了几个选择,但感觉没有一个是真正合适的 我有以下格式的跟踪文件:C++ 确定字符串是否包含日期和时间,c++,C++,所以,我正在做一个项目,我找到了几个选择,但感觉没有一个是真正合适的 我有以下格式的跟踪文件: 2018-08-09 09:30:34.118 Info 0 ... 2018-08-09 09:30:34.125 SystemInfo 0 ... 不同的跟踪文件应该是可比较的,所以需要删去日期和时间 到目前为止,我的程序是这样的: #include "targetver.h" #include <stdio.h> #include <tchar
2018-08-09 09:30:34.118 Info 0 ...
2018-08-09 09:30:34.125 SystemInfo 0 ...
不同的跟踪文件应该是可比较的,所以需要删去日期和时间
到目前为止,我的程序是这样的:
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <string>
#include <iostream>
#include <ctime>
using namespace std;
void main(string filename)
{
ifstream in(filename);
ofstream out;
string line, sub_line;
if (in.is_open())
{
out.open("stripped_" + filename);
while (getline(in, line))
{
if (in.good())
{
if ()
sub_line = line.substr(26);
out << sub_line;
in.close();
out.close();
}
}
}
else
{
cout << "File cannot be opened..." << endl;
}
}
#包括“targetver.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void main(字符串文件名)
{
ifstream-in(文件名);
流出的液体;
弦线、子线;
if(in.is_open())
{
out.open(“stripped_”+文件名);
while(getline(in,line))
{
如果(in.good())
{
如果()
sub_line=行substr(26);
out您可以使用正则表达式(regex)来查找字符串中的模式。
下面是如何在C++中使用正则表达式:
#include <regex>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string strs[] = { "2018-08-09 09:30:34.118", "Foo", "Hello" };
regex reg(R"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})");
for (auto & i : strs)
{
smatch m;
if (regex_search(i, m, reg))
cout << "Correct!" << endl;
else
cout << "Not correct" << endl;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串strs[]={“2018-08-09 09:30:34.118”,“Foo”,“Hello”};
正则表达式reg(R“(\d{4}-\d{2}-\d{2}\d{2}:\d{2}:\d{2}.\d{3})”;
用于(自动和输入:strs)
{
smatch m;
if(正则表达式搜索(i、m、reg))
cout可以使用>>解析中的为您解析毫秒精度的时间点(“%F%T”,tp)
.C++20为什么不删除每行的前28个字符呢?你甚至不需要为此编写自己的代码。在linux上,例如:cut-c29-logfile>logfile\u
@sanderedycker可能出于某种原因,他想通过编程来完成这项工作,