std::regx

2017. 8. 25. 09:09 from 셈말짓기/C and C++

#include "stdafx.h"

 

#include <regex>

#include <sstream>

#include <vector>

#include <iostream>

#include <vector>

 

void t4()

{

         //-----------------------------------------------------------------------

         std::string expression;

 

 

         //  ECMAScript : Special Keyword ^ $ \ . * + ? ( ) [ ] { } |

         expression = "\\\"([^\\\"]*)\\\"=([NR])";

         //       expression = R"STRING_LITERAL_DELIMITER(\"([^\"]*)\"=([NR]))STRING_LITERAL_DELIMITER";

         expression = R"d(\"([^\"]*)\"=([NR]))d";

         //       expression = R"d(\"([^\"]*)\")d";

 

 

         //-----------------------------------------------------------------------

         std::string text;

         std::string element;

 

         text = "  \"1A(:B/C)\":N, \"2AB,C\":R\"3A(:B/C)\":R, \"aa\"  ";

         text = "  \"21\"=N, \"22\"=R, \"23\"=R, \"24\"=x";

 

 

 

         //-----------------------------------------------------------------------

         std::vector<int> ei; // element index

 

 

         ei.push_back(1);

         ei.push_back(2);

         ei.push_back(3);

 

 

         //-----------------------------------------------------------------------

         std::regex                       r(expression);

         const std::sregex_token_iterator end;

         std::sregex_token_iterator       i(text.begin(), text.end(), r, ei);

 

 

         std::cout << expression << std::endl;

         std::cout << text << std::endl;

         while (i != end)

         {

                  element = *i++;

 

                  std::cout << element << std::endl;

         }

}

 

void t5()

{

         //-----------------------------------------------------------------------

         std::string expression;

 

 

         expression = R"d(\"([^\"]*)\"=([NR]))d";

 

 

         //-----------------------------------------------------------------------

         std::string text;

         std::string element;

 

 

         text = "  \"21\"=N, \"22\"=R, \"23\"=R, \"24\"=x";

 

 

         //-----------------------------------------------------------------------

         std::regex      r(expression);

         std::smatch     m;

         std::ssub_match sm;

 

         std::string value;

 

         std::size_t i;

         std::size_t count;

 

 

         value = text;

 

         while (std::regex_search(value, m, r))

         {

                  count = m.size();

 

                  for (i = 0u; i < count; i++)

                  {

                           sm = m[i];

 

                           if (sm.matched)

                           {

                                   element = sm.str();

 

                                   std::cout << i << " = " << element << std::endl;

                           }

                  }

 

                  value = m.suffix();

         }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

         t5();

 

 

         return 0;

}

 

Posted by 셈말짓기 :