std::regex_iterator
template< class BidirectionalIterator, |
(since C++11) | |
std::regex_iterator is a read-only ForwardIterator that accesses the individual matches of a regular expression within the underlying character sequence.
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<BidirectionalIterator>). 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.
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.
A typical implementation of std::regex_iterator holds the begin and the end iterators for the underlying sequence (two instances of BidirectionalIterator), 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<BidirectionalIterator>).
Several specializations for common character sequence types are defined:
Defined in header <regex>
| |
Type | 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> |
Contents |
[edit] Member types
Member type | Definition |
value_type | std::match_results<BidirectionalIterator> |
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> |
[edit] Member functions
constructs a new regex_iterator (public member function) | |
(destructor) (implicitly declared) |
destructs a regex_iterator, including the cached value (public member function) |
replaces a regex_iterator (public member function) | |
compares two regex_iterators (public member function) | |
obtains a reference to the current match accesses a member of the current match (public member function) | |
advances the regex_iterator to the next match (public member function) |
[edit] Notes
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.
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.
[edit] Example
#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'; }
Output:
The number of words is 3
[edit] See also
(C++11) |
identifies one regular expression match, including all sub-expression matches (class template) |
(C++11) |
check if a regular expression occurs anywhere within a string (function template) |