BOOST.REGEX定界器解析

Boost.Regex delimiter parsing

本文关键字:REGEX BOOST      更新时间:2023-10-16

这是一个快速的问题,我只是在Boost文档中找不到它或任何其他Boost Regex示例/教程。

假设我想使用此实现来将字符串归为字符串:

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;
}

问题在于,定界符表达式不会迭代。我还需要定界符字符串。我该如何确保?

如果我正确理解,除了通过令牌迭代之外,还要通过定义器迭代。创建另一个值得确定的令牌的令牌迭代器是否足够?

 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++ << ") ";
       }
   }