命名空间
变体
操作

std::regex_token_iterator

来自 cppreference.cn
< cpp‎ | regex
 
 
 
正则表达式库
(C++11)
算法
迭代器
regex_token_iterator
(C++11)
异常
特性
常量
(C++11)
正则表达式语法
 
 
定义于头文件 <regex>
template<

    class BidirIt,
    class CharT = typename std::iterator_traits<BidirIt>::value_type,
    class Traits = std::regex_traits<CharT>

> class regex_token_iterator
(自 C++11 起)

std::regex_token_iterator 是一个只读的 LegacyForwardIterator,它访问底层字符序列中正则表达式每次匹配的各个子匹配。它也可以用于访问序列中未被给定正则表达式匹配的部分(例如,作为分词器)。

在构造时,它构造一个 std::regex_iterator,并且在每次递增时,它会步进遍历来自当前 match_results 的请求的子匹配,当从最后一个子匹配递增离开时,会递增底层的 std::regex_iterator

默认构造的 std::regex_token_iterator 是序列末尾迭代器。当有效的 std::regex_token_iterator 在到达最后一次匹配的最后一个子匹配后递增时,它将变得等于序列末尾迭代器。进一步解引用或递增它会调用未定义行为。

就在变成序列末尾迭代器之前,如果索引 -1(未匹配片段)出现在请求的子匹配索引列表中,则 std::regex_token_iterator 可能会变成后缀迭代器。这样的迭代器,如果被解引用,则返回一个 match_results,对应于最后一次匹配和序列末尾之间的字符序列。

std::regex_token_iterator 的典型实现包含底层的 std::regex_iterator、一个请求的子匹配索引的容器(例如 std::vector<int>)、等于子匹配索引的内部计数器、指向 std::sub_match 的指针(指向当前匹配的当前子匹配)以及包含最后一个未匹配字符序列(在分词器模式中使用)的 std::match_results 对象。

内容

[编辑] 类型要求

-
BidirIt 必须满足 LegacyBidirectionalIterator 的要求。

[编辑] 特化

定义了针对常见字符序列类型的几种特化

定义于头文件 <regex>
类型 定义
std::cregex_token_iterator std::regex_token_iterator<const char*>
std::wcregex_token_iterator std::regex_token_iterator<const wchar_t*>
std::sregex_token_iterator std::regex_token_iterator<std::string::const_iterator>
std::wsregex_token_iterator std::regex_token_iterator<std::wstring::const_iterator>

[编辑] 成员类型

成员类型 定义
value_type std::sub_match<BidirIt>
difference_type std::ptrdiff_t
pointer const value_type*
reference const value_type&
iterator_category std::forward_iterator_tag
iterator_concept (C++20) std::input_iterator_tag
regex_type std::basic_regex<CharT, Traits>

[编辑] 成员函数

构造一个新的 regex_token_iterator
(公共成员函数) [编辑]
(析构函数)
(隐式声明)
析构一个 regex_token_iterator,包括缓存的值
(公共成员函数) [编辑]
赋值内容
(公共成员函数) [编辑]
(C++20 中移除)
比较两个 regex_token_iterator
(公共成员函数) [编辑]
访问当前子匹配
(公共成员函数) [编辑]
将迭代器前进到下一个子匹配
(公共成员函数) [编辑]

[编辑] 注解

程序员有责任确保传递给迭代器构造函数的 std::basic_regex 对象比迭代器寿命更长。因为迭代器存储了一个 std::regex_iterator,后者存储了一个指向 regex 的指针,所以在 regex 被销毁后递增迭代器会导致未定义行为。

[编辑] 示例

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <regex>
 
int main()
{
    // Tokenization (non-matched fragments)
    // Note that regex is matched only two times; when the third value is obtained
    // the iterator is a suffix iterator.
    const std::string text = "Quick brown fox.";
    const std::regex ws_re("\\s+"); // whitespace
    std::copy(std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
              std::sregex_token_iterator(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
 
    std::cout << '\n';
 
    // Iterating the first submatches
    const std::string html = R"(<p><a href="http://google.com">google</a> )"
                             R"(< a HREF ="http://cppreference.cn">cppreference</a>\n</p>)";
    const std::regex url_re(R"!!(<\s*A\s+[^>]*href\s*=\s*"([^"]*)")!!", std::regex::icase);
    std::copy(std::sregex_token_iterator(html.begin(), html.end(), url_re, 1),
              std::sregex_token_iterator(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
}

输出

Quick
brown
fox.
 
http://google.com
https://cppreference.cn

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3698
(P2770R0)
C++20 regex_token_iterator 是一个 forward_iterator
当作为一个暂存迭代器时
成为 input_iterator[1]
  1. iterator_category 未被此决议更改,因为将其更改为 std::input_iterator_tag 可能会破坏太多现有代码。