我有文件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 regex 并可以帮助我吗?
最佳答案
您可以使用这样的代码(改编自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';
}
关于c++ - 使用 boost::regex 更改文件中数据的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879138/