boost:regex分割字符串(带有'\'字符)

          在实际的应用中,经常用到boost:regex进行字符串的分割,特别是windows下的路径字符串的分割,由于windows的路径字符串带有特殊字符'\',boost:regex需要对此进行特殊处理,下面举例说明,分割字符串的正则表达式如下:

boost::regex reg("\\\\");

由于反斜杠对于c++字符和regex来说都是特殊字符,因而需要转义两次,才能正确执行。

            完整代码如下:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <boost/regex.hpp>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
         std::vector<std::string> vec;
         std::string s = "F:\\OtherProjects\\BasePlatForm\\C21\\SMC\\Backend\\Src";
         try
        {
              boost::regex reg("\\\\");
 
              boost::sregex_token_iterator it(s.begin(),s.end(),reg,-1);
              boost::sregex_token_iterator end;

  while(it != end)
  {
   vec.push_back(*it++);
  }

         }
        catch(boost::regex_error &err)
        {
              cout<<err.what()<<endl;
              cin.get();
              return 0;
         }
        catch(...)
       {
             cout<<"error"<<endl;
            cin.get();
            return 0;
        }
 
        assert(vec.size() == std::count(s.begin(),s.end(),'//')+1);

        for(int i =0;i<vec.size();i++)
        {
             cout<<vec[i]<<endl;
         }

       cin.get();
       return 0;
}

希望本文能给有同样问题的人,有点帮助~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值