在线文档教程
C++
规律表达 | Regular expressions

std::regex_iterator

STD::regex[医]迭代器

template< class BidirIt, class CharT = typename std::iterator_traits::value_type, class Traits = std::regex_traits > class regex_iterator(since C++11)

std::regex_iterator是只读ForwardIterator它访问底层字符序列中正则表达式的各个匹配项。

在构造和每一个增量上,它调用std::regex_search并记住结果%28,即保存值的副本。std::match_results<BidirIt>29%。当迭代器被构造或第一解引用完成时,可以读取第一对象。否则,取消引用只返回最近获得的regex匹配的副本。

默认构造std::regex_iterator是序列结束迭代器。当一个有效的std::regex_iterator在达到最后一次匹配%28后递增。std::regex_search回报false%29,它就等于序列结束迭代器.。解除引用或增量将进一步调用未定义的行为。

的典型实现std::regex_iterator保存Bidirit%29的底层序列%282个实例的开始迭代器和结束迭代器,这是指向正则表达式%28的指针。const regex_type*%29,匹配标志%28std::regex_constants::match_flag_type%29,当前匹配%28std::match_results<BidirIt>29%。

类型要求

-Bidirit必须符合双向迭代器的要求。

*。

专门性

定义了几种常见字符序列类型的专门化:

在标头中定义<regex>

*。

类型定义

克雷格[医]迭代器正则表达式[医]迭代器<Const char%2A>

冬青[医]迭代器正则表达式[医]迭代器<constwchar[医]T型%2A>

瑞吉[医]迭代器正则表达式[医]迭代器<std::string::const[医]迭代器>

瑞吉[医]迭代器正则表达式[医]迭代器<std::wstring::const[医]迭代器>

成员类型

Member typeDefinition
value_typestd::match_results<BidirIt>
difference_typestd::ptrdiff_t
pointerconst value_type*
referenceconst value_type&
iterator_categorystd::forward_iterator_tag
regex_typebasic_regex<CharT, Traits>

成员函数

(constructor)constructs a new regex_iterator (public member function)
(destructor) (implicitly declared)destructs a regex_iterator, including the cached value (public member function)
operator=assigns contents (public member function)
operator==operator!=compares two regex_iterators (public member function)
operator*operator->accesses the current match (public member function)
operator++operator++(int)advances the iterator to the next match (public member function)

注记

程序员%27的责任是确保std::basic_regex对象传递给迭代器%27s构造函数的。因为迭代器存储一个指向regex的指针,所以在regex被销毁后增加迭代器将访问一个悬空的指针。

如果匹配的正则表达式的部分只是断言%28^,,,$,,,\b,,,\B%29,迭代器中存储的匹配为零长度匹配,即,match[0].first == match[0].second...

二次

#include <regex> #include <iterator> #include <iostream> #include <string> int main() { const std::string s = "Quick brown fox."; std::regex words_regex("[^\\s]+" auto words_begin = std::sregex_iterator(s.begin(), s.end(), words_regex auto words_end = std::sregex_iterator( std::cout << "Found " << std::distance(words_begin, words_end) << " words:\n"; for (std::sregex_iterator i = words_begin; i != words_end; ++i) { std::smatch match = *i; std::string match_str = match.str( std::cout << match_str << '\n'; } }

二次

产出:

二次

Found 3 words: Quick brown fox.

二次

另见

match_results (C++11)identifies one regular expression match, including all sub-expression matches (class template)
regex_search (C++11)check if a regular expression occurs anywhere within a string (function template)

二次

*。

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/regex/regex[医]迭代器