Let m
be of type std::smatch
. Suppose there is an unmatched group i
. What is
m.position(i)
? For that matter, what is m[i]
?
For example, consider
std::regex re {"^(a+)|(b+)"};
string target="aa";
std::smatch m;
std::regex_search(target,m,re);
cout<<"m[2] is: "<<m[2]<<" at position: "<<m.position(2);
I cannot figure out from the reference https://en.cppreference.com/w/cpp/regex/match_results/position what is guaranteed to happen here and why.
According to the C++17 Standard:
28.10
Class template match_results
[re.results
]4 The sub_match object stored at index 0 represents sub-expression 0, i.e., the whole match. In this case the sub_match member matched is always true. The sub_match object stored at index n denotes what matched the marked sub-expression n within the matched expression. If the sub-expression n participated in a regular expression match then the sub_match member matched evaluates to true, and members first and second denote the range of characters [first,second) which formed that match. Otherwise matched is false, and members first and second point to the end of the sequence that was searched.
[ Note: The sub_match objects representing different sub-expressions that did not participate in a regular expression match need not be distinct. — end note ]
Now m.position(n)
returns (*this)[n].first
.
Given that "[If] matched is false, [then] members first and second point to the end of the sequence that was searched" ...
This means m.position(n)
should point "to the end of the sequence that was searched".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With