c++ - Boost.Regex 分隔符解析

标签 c++ regex boost delimiter

这是一个相当快速的问题,我在 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;
}

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

最佳答案

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

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

关于c++ - Boost.Regex 分隔符解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13179311/

相关文章:

c++ - 使用波兰语字符时文件内容被截断

java - 归并排序的开放区间的中点

c++ - 在循环中使用 std::list 的删除方法会创建段错误

regex - 替换无效的文件名字符

php regex - 干净的文件名

c++ - 如何将 socket.async_read_some 的读取处理程序调用同步到特定频率?

c++ - 无法从 boost::asio::io_service::run 捕获异常

c++ - 二叉搜索树验证的空间复杂度

python - 如何仅使用正则表达式提取数字?

c++ - boost库应该依赖于结构成员对齐吗?