【C++】regex_search 匹配字符串出错

regex_search 在处理特殊中文的时候会出错。

直接上代码:

#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::string sx ("0,虞圣向其他玩家发送交易请求?,name:虞圣,mood:还没想好!,petname:花仙子宝宝");
  //std::smatch mx;
  //std::smatch mx_1;
  std::regex ex (".*玩.*服"); 
  std::regex ex_1 (".*玩.*菔"); 
  std::regex ex_2 (".*交.*易"); 
  std::regex ex_3 (".*交.*上"); 

  bool res = std::regex_search (sx,ex);
  std::cout <<res<< std::endl;

  bool res2 = std::regex_search (sx,ex_1);
  std::cout <<res2<< std::endl;
  
  bool res3 = std::regex_search (sx,ex_2);
  std::cout <<res3<< std::endl;

   bool res4 = std::regex_search (sx,ex_3);
  std::cout <<res4<< std::endl;





  /*
  while (std::regex_search (sx,mx,ex)) {
    for (auto x:mx) std::cout << x << " ";
    std::cout << std::endl;
    s = mx.suffix().str();
  }

  


  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;
 

  while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  } */
  system("pause");
  return 0;
}


运行结果如下:

 很奇怪微软是怎么实现的,先mark下,等有时间了再去看源码。

猜你喜欢

转载自www.cnblogs.com/sdu-Jumper/p/12018693.html
今日推荐