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

Change format of data in file using boost::regex

本文关键字:数据 文件 格式 boost regex 使用      更新时间:2023-10-16

>我有文件输出.txt,在文件中我们可以找到类似以下内容的内容:

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

我想使用 Boost Regex 将格式从 DD/MM/RRRR HH:MM:SS 更改为 RRRR-MM-DD HH:MM:SS.000。但是我不知道应该如何键入模式。

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

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

#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';
}

正则表达式正在做什么的演示