std::regex_token_iterator
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
template< class BidirIt, |
(C++11 起) | |
std::regex_token_iterator
是一个只读的ForwardIterator
访问的各个子匹配正则表达式的的底层字符序列内的每场比赛。它也可以被用于访问由给定的正则表达式(例如,作为一个tokenizer)不匹配的部分的序列.原文:
std::regex_token_iterator
is a read-only ForwardIterator
that accesses the individual sub-matches of every match of a regular expression within the underlying character sequence. It can also be used to access the parts of the sequence that were not matched by the given regular expression (e.g. as a tokenizer).在建筑上,它构造了一个std::regex_iterator每增加步骤,通过请求的子匹配从目前的match_results,递增的基础regex_iterator递增距离最后的个子匹配时,.
原文:
On construction, it constructs an std::regex_iterator and on every increment it steps through the requested sub-matches from the current match_results, incrementing the underlying regex_iterator when incrementing away from the last submatch.
默认构造
std::regex_token_iterator
是最终的序列迭代器。当一个有效的std::regex_token_iterator
后递增到达最后的submatch的最后匹配,它变得等于序列结束迭代器。提领或递增的进一步调用未定义的行为.原文:
The default-constructed
std::regex_token_iterator
is the end-of-sequence iterator. When a valid std::regex_token_iterator
is incremented after reaching the last submatch of the last match, it becomes equal to the end-of-sequence iterator. Dereferencing or incrementing it further invokes undefined behavior.成为最终的序列的迭代器,前std::regex_token_iterator可能成为一个“后缀迭代器”,如果该指数-1(非匹配片段)出现在列表中的子匹配的指标。这样的迭代器,如果废弃时,返回一个match_results相应的字符序列之间的最后一场比赛结束的序列.
原文:
Just before becoming the end-of-sequence iterator, a std::regex_token_iterator may become a suffix iterator, if the index -1 (non-matched fragment) appears in the list of the requested submatch indexes. Such iterator, if dereferenced, returns a match_results corresponding to the sequence of characters between the last match and the end of sequence.
一个典型的实施
std::regex_token_iterator
持有底层std::regex_iterator的请求的submatch索引,一个容器(例如std::vector<int>),内部计数器等于子匹配的索引,指针std::match_results指向当前匹配当前的submatch,和一个std::match_results对象,其中包含的最后一个非匹配的字符序列(用来标记生成模式).原文:
A typical implementation of
std::regex_token_iterator
holds the underlying std::regex_iterator, a container (e.g. std::vector<int>) of the requested submatch indexes, the internal counter equal to the index of the submatch, a pointer to std::match_results, pointing at the current submatch of the current match, and a std::match_results object containing the last non-matched character sequence (used in tokenizer mode).目录 |
[编辑] 类型要求
-BidirIt 必须满足 BidirectionalIterator 的要求。
|
[编辑] 专业化
常见的字符序列类型的几个专业的定义
原文:
Several specializations for common character sequence types are defined:
在头文件
<regex> 中定义 | |
类型
|
Definition |
cregex_token_iterator
|
regex_token_iterator<const char*> |
wcregex_token_iterator
|
regex_token_iterator<const wchar_t*> |
sregex_token_iterator
|
regex_token_iterator<std::string::const_iterator> |
wsregex_token_iterator
|
regex_token_iterator<std::wstring::const_iterator> |
[编辑] 会员类型
会员类型
|
Definition |
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 |
regex_type
|
basic_regex<CharT, Traits> |
[编辑] 成员函数
构造一个新的regex_token_iterator 原文: constructs a new regex_token_iterator (公共成员函数) | |
(destructor) (隐式声明) |
destructs a regex_token_iterator, including the cached value (公共成员函数) |
取代了regex_token_iterator (公共成员函数) | |
比较2 regex_token_iterators (公共成员函数) | |
accsses当前的submatch (公共成员函数) | |
regex_token_iterator推进到下一个子匹配的 原文: advances the regex_token_iterator to the next submatch (公共成员函数) |
[编辑] 注释
这是程序员的责任,以确保std::basic_regex对象传递给迭代器的构造函数会超越的迭代器。因为迭代器存储std::regex_iterator存储一个指向正则表达式,递增迭代器的正则表达式被破坏后导致未定义的行为.
原文:
It is the programmer's responsibility to ensure that the std::basic_regex object passed to the iterator's constructor outlives the iterator. Because the iterator stores a std::regex_iterator which stores a pointer to the regex, incrementing the iterator after the regex was destroyed results in undefined behavior.
[编辑] 示例
#include <fstream> #include <iostream> #include <algorithm> #include <iterator> #include <regex> int main() { std::string text = "Quick brown fox."; // tokenization (non-matched fragments) // Note that regex is matched only two times: when the third value is obtained // the iterator is a suffix iterator. 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")); // iterating the first submatches std::string html = "<p><a href=\"http://google.com\">google</a> " "< a HREF =\"http://cppreference.com\">cppreference</a>\n</p>"; std::regex url_re("<\\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 http://cppreference.com