DDD
DDD

Reputation: 121

Change format of data in file using boost::regex

I have file output.txt, in file we can find something like:

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

and i would like to change format from DD/MM/RRRR HH:MM:SS to RRRR-MM-DD HH:MM:SS.000 using boost regex. But I have no clue how should I type pattern.

Anyone use boost regex and could help me?

Upvotes: 2

Views: 176

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You can use code like this (adaptation of an example at 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

Upvotes: 3

Related Questions