sub_match::length
Returns the length of a submatch.
Syntax
difference_type length() const;
Remarks
The member function returns the length of the matched sequence, or zero if there was no matched sequence.
Example
// 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
Requirements
Header: <regex>
Namespace: std