C++中用字符串来切割字符串

      参考之前写的:http://blog.sina.com.cn/s/blog_48d4cf2d0100r9hv.html。主要是增加一个regexEscape函数,免得只想使用字符串而不是正则表达式时数反斜杠数得眼花。有写几个测试用例,应该没啥大问题。
     
=======================代码的分割线================================
#include <string>
#include <vector>
#include <boost/regex.hpp>

namespace tool
{
      static const boost::regex esc("[\\^\\.\\$\\|\\(\\)\\[\\]\\{\\}\\*\\+\\?\\/\\\\]");
     
      static const std::string rep("\\\\\\1&");
     
      std::string regexEscape(const std::string& text)
      {
            std::string result;
            result = boost::regex_replace(text, esc, rep, boost::match_default | boost::format_sed);
            return result;     
      }

      void splitByStr(const std::string& str, std::vector<std::string>& results, const std::string& seperator)
      {
            splitByRegex(str, results, regexEscape(seperator));
      }

      void splitByRegex(const std::string& str, std::vector<std::string>& results, const std::string& regex)
      {
            boost::regex reg(regex);
            boost::sregex_token_iterator it(str.begin(), str.end(), reg, -1);
            boost::sregex_token_iterator end;
            while (it != end)
            {
                  results.push_back(*it++);
            }
      }

}


     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值