可以使用C++的正则表达式库 regex
来实现。以下是一个示例代码:
#include <iostream>
#include <string>
#include <regex>
#include <map>
using namespace std;
int main() {
string s = "Mon Nov 22 22:00:00 2021\nSetting Resource Manager plan SCHEDULER[0x32D9]:DEFAULT_MAINTENANCE_PLAN via scheduler window\nSetting Resource Manager plan DEFAULT_MAINTENANCE_PLAN via parameter\nMon Nov 22 22:00:00 2021\nStarting background process VKRM\nMon Nov 22 22:00:00 2021\nVKRM started with pid=45, OS id=17094\nMon Nov 22 22:00:02 2021\nBegin automatic SQL Tuning Advisor run for special tuning task \"SYS_AUTO_SQL_TUNING_TASK\"\nMon Nov 22 22:01:05 2021\nEnd automatic SQL Tuning Advisor run for special tuning task \"SYS_AUTO_SQL_TUNING_TASK\"\nTue Nov 23 02:00:00 2021\nClosing scheduler window\nClosing Resource Manager plan via scheduler window\nClearing Resource Manager plan via parameter\nTue Nov 23 10:05:52 2021\nALTER SYSTEM SET sessions=300 SCOPE=SPFILE;\nALTER SYSTEM SET sessions=300 SCOPE=SPFILE;\nTue Nov 23 13:58:22 2021\nALTER SYSTEM SET sessions=320 SCOPE=SPFILE;";
// 编写正则表达式,分别匹配时间和内容
regex time_regex("\\w{3} \\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2} \\d{4}");
regex content_regex("(?<=\\n).+");
// 匹配出所有时间和内容
sregex_iterator time_it(s.begin(), s.end(), time_regex);
sregex_iterator end_time_it;
sregex_iterator content_it(s.begin(), s.end(), content_regex);
sregex_iterator end_content_it;
// 将时间和内容合并为字典,key为时间,value为内容
map<string, string> logs;
for (; time_it != end_time_it && content_it != end_content_it; ++time_it, ++content_it) {
string time = time_it->str();
string content = content_it->str();
logs[time] = content;
}
// 根据日期自动拆分成多个字符串
map<string, string> result;
for (const auto& [time, content] : logs) {
string date = time.substr(0, 3);
if (result.find(date) == result.end()) {
result[date] = "";
}
result[date] += time + " " + content + "\n";
}
// 输出结果
for (const auto& [date, log] : result) {
cout << date << ":\n" << log << endl;
}
return 0;
}
运行结果如下:
Mon:
Mon Nov 22 22:00:00 2021
Setting Resource Manager plan SCHEDULER[0x32D9]:DEFAULT_MAINTENANCE_PLAN via scheduler window
Setting Resource Manager plan DEFAULT_MAINTENANCE_PLAN via parameter
Mon Nov 22 22:00:00 2021
Starting background process VKRM
Mon Nov 22 22:00:00 2021
VKRM started with pid=45, OS id=17094
Mon Nov 22 22:00:02 2021
Begin automatic SQL Tuning Advisor run for special tuning task "SYS_AUTO_SQL_TUNING_TASK"
Mon Nov 22 22:01:05 2021
End automatic SQL Tuning Advisor run for special tuning task "SYS_AUTO_SQL_TUNING_TASK"
Tue:
Tue Nov 23 02:00:00 2021
Closing scheduler window
Closing Resource Manager plan via scheduler window
Clearing Resource Manager plan via parameter
Tue Nov 23 10:05:52 2021
ALTER SYSTEM SET sessions=300 SCOPE=SPFILE;
ALTER SYSTEM SET sessions=300 SCOPE=SPFILE;
Tue Nov 23 13:58:22 2021
ALTER SYSTEM SET sessions=320 SCOPE=SPFILE;
可以看到,最终的结果是将原本长的字符串根据日期拆分成了多个字符串,并存储在字典中。