【问题标题】:Boost.Regex delimiter parsingBoost.Regex 分隔符解析
【发布时间】:2012-11-01 14:35:29
【问题描述】:

这是一个相当简单的问题,我只是在 boost 文档或任何其他 boost 正则表达式示例/教程中都找不到它。

假设我想使用这个实现标记一个字符串:

boost::regex re("[\\sXY]+");
std::string s;

while (std::getline(std::cin, s)) {
  boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
boost::sregex_token_iterator j;
  while (i != j) {
     std::cout << *i++ << " ";
  }
  std::cout << std::endl;
}

问题是分隔符表达式不会被迭代。我还需要分隔符字符串。我怎样才能确定这一点?

【问题讨论】:

    标签: c++ regex boost delimiter


    【解决方案1】:

    如果我理解正确,除了遍历标记之外,您还想遍历分隔符。创建另一个令牌迭代器来查找由您的正则表达式标识的令牌还不够吗?

     boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
    
     boost::sregex_token_iterator j;
     //now find the tokens that match the regex -> the delimiters
     boost::sregex_token_iterator begin(s.begin(), s.end(), re), end;
     while (i != j)
       {
         std::cout << *i++ << " ";
         if( begin != end)
           {
             std::cout << "(delimiter = " << *begin++ << ") ";
           }
       }
    

    【讨论】:

    • 是的,但是现在分隔符和其他标记的顺序不正确...我想拆分一个字符串,但分隔符必须在其他标记之间并且 必须 顺序正确。
    • 对不起,你完全正确!每个标记都由分隔符分隔。谢谢!
    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多