std::regex_iterator

From Cppreference

Jump to: navigation, search
template<

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

> class regex_iterator
(since C++11)

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>

[edit] Example

#include <regex>
#include <iterator>
#include <iostream>
int main()
{
    const std::string text = "Quick brown fox.";
 
    std::cout << "The number of words is " <<
        std::distance(
            std::sregex_iterator(text.begin(), text.end(), std::regex("[^\\s]+")),
            std::sregex_iterator()
        ) << '\n';
}

Output:

The number of words is 3