Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

match_results::size

Counts number of submatches.

size_type size() const;

The member function returns one more than the number of capture groups in the regular expression that was used for the search, or zero if no search has been made.

 

// std_tr1__regex__match_results_size.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 
 
int main() 
    { 
    std::tr1::regex rx("c(a*)|(b)"); 
    std::tr1::cmatch mr; 
 
    std::tr1::regex_search("xcaaay", mr, rx); 
 
    std::cout << "prefix: matched == " << std::boolalpha 
        << mr.prefix().matched 
        << ", value == " << mr.prefix() << std::endl; 
    std::cout << "whole match: " << mr.length() << " chars, value == " 
        << mr.str() << std::endl; 
    std::cout << "suffix: matched == " << std::boolalpha 
        << mr.suffix().matched 
        << ", value == " << mr.suffix() << std::endl; 
    std::cout << std::endl; 
 
    std::string fmt("\"c(a*)|(b)\" matched \"$0\"\n" 
        "\"(a*)\" matched \"$1\"\n" 
        "\"(b)\" matched \"$2\"\n"); 
    std::cout << mr.format(fmt) << std::endl; 
    std::cout << std::endl; 
 
// index through submatches 
    for (size_t n = 0; n < mr.size(); ++n) 
        { 
        std::cout << "submatch[" << n << "]: matched == " << std::boolalpha 
            << mr[n].matched << 
            " at position " << mr.position(n) << std::endl; 
        std::cout << "  " << mr.length(n) 
            << " chars, value == " << mr[n] << std::endl; 
        } 
    std::cout << std::endl; 
 
// iterate through submatches 
    for (std::tr1::cmatch::iterator it = mr.begin(); it != mr.end(); ++it) 
        { 
        std::cout << "next submatch: matched == " << std::boolalpha 
            << it->matched << std::endl; 
        std::cout << "  " << it->length() 
            << " chars, value == " << *it << std::endl; 
        } 
    std::cout << std::endl; 
 
// other members 
    std::tr1::cmatch mr1(mr); 
    mr = mr1; 
    mr.swap(mr1); 
 
    char buf[10]; 
    *mr.format(&buf[0], "<$0>") = '\0'; 
    std::cout << &buf[0] << std::endl; 
    std::cout << "empty == " << std::boolalpha << mr.empty() << std::endl; 
 
    std::tr1::cmatch::allocator_type al = mr.get_allocator(); 
    std::tr1::cmatch::string_type str = std::string("x"); 
    std::tr1::cmatch::size_type maxsiz = mr.max_size(); 
    std::tr1::cmatch::char_type ch = 'x'; 
    std::tr1::cmatch::difference_type dif = mr.begin() - mr.end(); 
    std::tr1::cmatch::const_iterator cit = mr.begin(); 
    std::tr1::cmatch::value_type val = *cit; 
    std::tr1::cmatch::const_reference cref = val; 
    std::tr1::cmatch::reference ref = val; 
 
    maxsiz = maxsiz;  // to quiet "unused" warnings 
    if (ref == cref) 
        ch = ch; 
    dif = dif; 
 
    return (0); 
    } 
 
prefix: matched == true, value == x whole match: 4 chars, value == caaa suffix: matched == true, value == y "c(a*)|(b)" matched "caaa" "(a*)" matched "aaa" "(b)" matched "" submatch[0]: matched == true at position 1 4 chars, value == caaa submatch[1]: matched == true at position 2 3 chars, value == aaa submatch[2]: matched == false at position 6 0 chars, value == next submatch: matched == true 4 chars, value == caaa next submatch: matched == true 3 chars, value == aaa next submatch: matched == false 0 chars, value == <caaa> empty == false

Header: <regex>

Namespace: std::tr1

Community Additions

ADD
Show:
© 2015 Microsoft