Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
sub_match::length

sub_match::length

Returns the length of a submatch.

difference_type length() const;

The member function returns the length of the matched sequence, or zero if there was no matched sequence.

 

// std_tr1__regex__sub_match_length.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 
 
int main() 
    { 
    std::regex rx("c(a*)|(b)"); 
    std::cmatch mr; 
 
    std::regex_search("xcaaay", mr, rx); 
 
    std::csub_match sub = mr[1]; 
    std::cout << "matched == " << std::boolalpha 
        << sub.matched << std::endl; 
    std::cout << "length == " << sub.length() << std::endl; 
 
    std::csub_match::difference_type dif = std::distance(sub.first, sub.second); 
    std::cout << "difference == " << dif << std::endl; 
 
    std::csub_match::iterator first = sub.first; 
    std::csub_match::iterator last = sub.second; 
    std::cout << "range == " << std::string(first, last) 
        << std::endl; 
    std::cout << "string == " << sub << std::endl; 
 
    std::csub_match::value_type *ptr = "aab"; 
    std::cout << "compare(\"aab\") == " 
        << sub.compare(ptr) << std::endl; 
    std::cout << "compare(string) == " 
        << sub.compare(std::string("AAA")) << std::endl; 
    std::cout << "compare(sub) == " 
        << sub.compare(sub) << std::endl; 
 
    return (0); 
    } 
 
matched == true
length == 3
difference == 3
range == aaa
string == aaa
compare("aab") == -1
compare(string) == 1
compare(sub) == 0

Header: <regex>

Namespace: std

Show:
© 2015 Microsoft