Advertisement
Naohiro19

Boost.Xpressive + Boost.DateTime ( Semantic Actions)

Feb 7th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. #include <boost/xpressive/xpressive_static.hpp>
  6. #include <boost/xpressive/regex_actions.hpp>
  7. #include <boost/date_time/gregorian/gregorian.hpp>
  8.  
  9. using namespace boost::xpressive;
  10. using namespace boost::gregorian;
  11.  
  12.  
  13. int main()
  14. {
  15.     std::string input = "今日の日付は=YEAR=年=MONTH=月=DAY=日です。";
  16.  
  17.     // 本日の日付を取得
  18.     date today = day_clock::local_day();
  19.  
  20.     // 取得した日付を文字列に変換
  21.     std::string y = std::to_string(today.year());
  22.     std::string m = std::to_string(today.month());
  23.     std::string d = std::to_string(today.day());
  24.  
  25.     // 正規表現の対応表
  26.     std::map<std::string, std::string> env;
  27.     env["YEAR"] = y;
  28.     env["MONTH"] = m;
  29.     env["DAY"] = d;
  30.  
  31.     // = で始まって 大文字が1回以上続き = で終わる文字列を検索し、置き換える
  32.     sregex envar = '=' >> (s1 = +upper) >> '=';
  33.     std::string output = regex_replace(input, envar, boost::xpressive::ref(env)[s1]);
  34.  
  35.     // 結果を表示
  36.     std::cout << output << std::endl;
  37.     // → 今日の日付は2019年2月7日です。
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement