std::regex_iterator
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
template< class BidirIt, |
(C++11 起) | |
std::regex_iterator
是一个只读ForwardIterator
访问个人的正则表达式匹配的的底层字符序列内.原文:
std::regex_iterator
is a read-only ForwardIterator
that accesses the individual matches of a regular expression within the underlying character sequence.建设,并在每一个增量,它会调用std::regex_search和记忆的结果(即,节省了拷贝的价值std::match_results<BidirIt>)。该迭代器时建造或完成的第一个间接引用,可以读出的第一个对象。否则,只提领返回最近获得的正则表达式匹配的副本.
原文:
On construction, and on every increment, it calls std::regex_search and remembers the result (that is, saves a copy of the value std::match_results<BidirIt>). The first object may be read when the iterator is constructed or when the first dereferencing is done. Otherwise, dereferencing only returns a copy of the most recently obtained regex match.
默认构造
std::regex_iterator
是最终的序列迭代器。当一个有效的std::regex_iterator
递增到达最后一个如同(std::regex_search回报false)后,它变得等于序列结束迭代器。提领或递增的进一步调用未定义的行为.原文:
The default-constructed
std::regex_iterator
is the end-of-sequence iterator. When a valid std::regex_iterator
is incremented after reaching the last match (std::regex_search returns false), it becomes equal to the end-of-sequence iterator. Dereferencing or incrementing it further invokes undefined behavior.一个典型的实施
std::regex_iterator
持有的开始和结束迭代器基础序列(两个实例BidirIt),正则表达式(const regex_type*)和匹配标志(std::regex_constants::match_flag_type)的指针,和的电流匹配(std::match_results<BidirIt>).原文:
A typical implementation of
std::regex_iterator
holds the begin and the end iterators for the underlying sequence (two instances of BidirIt), a pointer to the regular expression (const regex_type*) and the match flags (std::regex_constants::match_flag_type), and the current match (std::match_results<BidirIt>).目录 |
[编辑] 类型要求
-BidirIt 必须满足 BidirectionalIterator 的要求。
|
[编辑] 专业化
常见的字符序列类型的几个专业的定义
原文:
Several specializations for common character sequence types are defined:
在头文件
<regex> 中定义 | |
类型
|
Definition |
cregex_iterator
|
regex_iterator<const char*> |
wcregex_iterator
|
regex_iterator<const wchar_t*> |
sregex_iterator
|
regex_iterator<std::string::const_iterator> |
wsregex_iterator
|
regex_iterator<std::wstring::const_iterator> |
[编辑] 会员类型
会员类型
|
Definition |
value_type
|
std::match_results<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_iterator (公共成员函数) | |
(destructor) (隐式声明) |
destructs a regex_iterator, including the cached value (公共成员函数) |
代替一个regex_iterator (公共成员函数) | |
比较2 regex_iterators (公共成员函数) | |
获得的电流的参考match accesses当前匹配的成员 原文: obtains a reference to the current match accesses a member of the current match (公共成员函数) | |
regex_iterator前进到下一个匹配 原文: advances the regex_iterator to the next match (公共成员函数) |
[编辑] 注释
这是程序员的责任,以确保std::basic_regex对象传递给迭代器的构造函数会超越的迭代器。因为迭代器存储的指针递增迭代器被破坏后的正则表达式正则表达式,访问一个悬摆指针.
原文:
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 pointer to the regex, incrementing the iterator after the regex was destroyed accesses a dangling pointer.
如果匹配正则表达式的一部分,只是一个断言(
^
,$
,\b
,\B
),存储在比赛中的迭代器是一个零长度的匹配,也就是说,match[0].first == match[0].second的原文:
If the part of the regular expression that matched is just an assertion (
^
, $
, \b
, \B
), the match stored in the iterator is a zero-length match, that is, match[0].first == match[0].second.[编辑] 示例
#include <regex> #include <iterator> #include <iostream> int main() { const std::string text = "Quick brown fox."; std::regex re("[^\\s]+"); auto beg = std::sregex_iterator(text.begin(), text.end(), re); auto end = std::sregex_iterator(); std::cout << "The number of words is " << std::distance(beg, end) << '\n'; }
输出:
The number of words is 3
[编辑] 另请参阅
(C++11) |
确定一个正则表达式匹配,包括所有的子表达式匹配 原文: identifies one regular expression match, including all sub-expression matches (类模板) |
(C++11) |
检查正则表达式在字符串中的任何地方发生 原文: check if a regular expression occurs anywhere within a string (函数模板) |