std::match_results::operator[]

From Cppreference

Jump to: navigation, search
const_reference operator[]( size_type n ) const;
(C++11 feature)

Returns an indicated sub_match. The sub_match 0 represents the entire matched expression, and an index >= size() will return a sub_match representing an unmatched match.

[edit] Parameters

(none)

[edit] Return value

Returns a sub_match representing the indicated match or sub match.

[edit] Example

#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::regex re("a(a)*b");
    std::string target("baaaby");
    std::smatch sm;
 
    std::regex_search(target, sm, re);
    std::cout << sm[1] << '\n';
}

Output:

aa