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