zoukankan      html  css  js  c++  java
  • 【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下,等有时间了再去看源码。

    ya tudou
  • 相关阅读:
    图解测试之稳定性-如何开始稳定性测试
    系统稳定性保障
    系统稳定性评测
    分布式架构的架构稳定性
    app测试--稳定性测试
    服务器稳定性测试方法汇总
    服务端稳定性测试
    发票问题
    android x86 固件定制
    Nim游戏博弈(收集完全版)
  • 原文地址:https://www.cnblogs.com/sdu-Jumper/p/12018693.html
Copyright © 2011-2022 走看看