Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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::tr1::regex rx("c(a*)|(b)"); 
    std::tr1::cmatch mr; 
 
    std::tr1::regex_search("xcaaay", mr, rx); 
 
    std::tr1::csub_match sub = mr[1]; 
    std::cout << "matched == " << std::boolalpha 
        << sub.matched << std::endl; 
    std::cout << "length == " << sub.length() << std::endl; 
 
    std::tr1::csub_match::difference_type dif = std::distance(sub.first, sub.second); 
    std::cout << "difference == " << dif << std::endl; 
 
    std::tr1::csub_match::iterator first = sub.first; 
    std::tr1::csub_match::iterator last = sub.second; 
    std::cout << "range == " << std::string(first, last) 
        << std::endl; 
    std::cout << "string == " << sub << std::endl; 
 
    std::tr1::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::tr1

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.