sub_match Class
The latest version of this topic can be found at sub_match Class.
Describes a submatch.
Syntax
template <class BidIt>
class sub_match
: public std::pair<BidIt, BidIt> {
public:
bool matched;
int compare(const sub_match& right) const;
int compare(const basic_string<value_type>& right) const;
int compare(const value_type *right) const;
difference_type length() const;
operator basic_string<value_type>() const;
basic_string<value_type> str() const;
typedef typename iterator_traits<BidIt>::value_type value_type;
typedef typename iterator_traits<BidIt>::difference_type difference_type;
typedef BidIt iterator;
};
Parameters
BidIt
The iterator type for submatches.
Remarks
The template class describes an object that designates a sequence of characters that matched a capture group in a call to regex_match Function or to regex_search Function. Objects of type match_results Class hold an array of these objects, one for each capture group in the regular expression that was used in the search.
If the capture group was not matched the object's data member matched
holds false, and the two iterators first
and second
(inherited from the base std::pair
) are equal. If the capture group was matched, matched
holds true, the iterator first
points to the first character in the target sequence that matched the capture group, and the iterator second
points one position past the last character in the target sequence that matched the capture group. Note that for a zero-length match the member matched
holds true, the two iterators will be equal, and both will point to the position of the match.
A zero-length match can occur when a capture group consists solely of an assertion, or of a repetition that allows zero repeats. For example:
"^" matches the target sequence "a"; the sub_match
object corresponding to capture group 0 holds iterators that both point to the first character in the sequence.
"b(a*)b" matches the target sequence "bb"; the sub_match
object corresponding to capture group 1 holds iterators that both point to the second character in the sequence.
Requirements
Header: <regex>
Namespace: std
sub_match::compare
Compare submatch against a sequence.
int compare(const sub_match& right) const;
int compare(const basic_string<value_type>& str) const;
int compare(const value_type *ptr) const;
Parameters
right
The submatch to compare to.
str
The string to compare to.
ptr
The nul-terminated sequence to compare to.
Remarks
The first member function compares the matched sequence [first, second)
to the matched sequence [right.first, right.second)
. The second member function compares the matched sequence [first, second)
to the character sequence [right.begin(), right.end())
. The third member function compares the matched sequence [first, second)
to the character sequence [right, right + std::char_traits<value_type>::length(right))
.
Each function returns:
a negative value if the first differing value in the matched sequence compares less than the corresponding element in the operand sequence (as determined by std::char_traits<value_type>::compare
), or if the two have a common prefix but the target sequence is longer
zero if the two compare equal element by element and have the same length
a positive value otherwise
Example
// std_tr1__regex__sub_match_compare.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
sub_match::difference_type
The type of an iterator difference.
typedef typename iterator_traits<BidIt>::difference_type difference_type;
Remarks
The typedef is a synonym for iterator_traits<BidIt>::difference_type
.
Example
// std_tr1__regex__sub_match_difference_type.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
sub_match::iterator
The type of an iterator.
typedef BidIt iterator;
Remarks
The typedef is a synonym for the template type argument Bidit
.
Example
// std_tr1__regex__sub_match_iterator.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
sub_match::length
Returns the length of a submatch.
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
sub_match::matched
Indicates if match succeeded.
bool matched;
Remarks
The member holds true
only if the capture group associated with *this
was part of the regular expression match.
Example
// std_tr1__regex__sub_match_matched.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
sub_match::operator basic_string<value_type>
Casts submatch to a string.
operator basic_string<value_type>() const;
Remarks
The member operator returns str()
.
Example
// std_tr1__regex__sub_match_operator_str.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
sub_match::str
Converts submatch to a string.
basic_string<value_type> str() const;
Remarks
The member function returns basic_string<value_type>(first, second)
.
Example
// std_tr1__regex__sub_match_str.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
sub_match::value_type
The type of an element.
typedef typename iterator_traits<BidIt>::value_type value_type;
Remarks
The typedef is a synonym for iterator_traits<BidIt>::value_type
.
Example
// std_tr1__regex__sub_match_value_type.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