gpt4 book ai didi

c++ - 使用 boost::regex 更改文件中的数据格式

转载 作者:搜寻专家 更新时间:2023-10-31 01:40:22 27 4
gpt4 key购买 nike

我有文件 output.txt,在文件中我们可以找到如下内容:

01/01/2015  15:00:00      2222.2222
2222.2222 2222.2222 2222.2222
04/04/2015 15:00:00 2222.2222

我想使用 boost 将格式从 DD/MM/RRRR HH:MM:SS 更改为 RRRR-MM-DD HH:MM:SS.000正则表达式。但我不知道应该如何输入模式。

有人使用 boost 正则表达式可以帮助我吗?

最佳答案

您可以使用这样的代码(改编自 Boost C++ Libraries 中的示例):

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
std::string s = "01/30/2015 15:00:00 2222.2222";
boost::regex expr{"([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})\\s+([0-9]{2}):([0-9]{2}):([0-9]{2})\\s+[0-9]{4}\\.[0-9]{4}"};
std::string fmt{"\\3/\\1/\\2 \\4:\\5:\\6.000"};
std::cout << boost::regex_replace(s, expr, fmt) << '\n';
}

A demo of what the regex is doing

关于c++ - 使用 boost::regex 更改文件中的数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com