What does std::match_results::size return, and why doesn't it correspond to the number of matches?
The std::match_results::size function in C 11 returns the number of capture groups plus one for the entire match value. In the code you provided:
<code class="cpp">#include <iostream> #include <string> #include <regex> int main() { std::string haystack("abcdefabcghiabc"); std::regex needle("abc"); std::smatch matches; std::regex_search(haystack, matches, needle); std::cout << matches.size() << std::endl; }
you might expect the output to be 3, since the string "abc" appears three times in the haystack. However, the output is actually 1. This is because regex_search only returns one match, and size() returns the number of capture groups plus the whole match value.
In this case, there are no capture groups, so size() returns 1 for the whole match value. If you want to find all matches of "abc" in the haystack, you can use the std::regex_search function with multiple parameters:
<code class="cpp">int main() { std::string haystack("abcdefabcghiabc"); std::regex needle("abc"); std::vector<std::smatch> matches; std::regex_search(haystack, needle, matches, std::regex_constants::match_continuous); std::cout << matches.size() << std::endl; }
This code will search the haystack for all matches of "abc" and store them in the matches vector. The size() function will then return the number of matches found, which is 3 in this case.
Alternative solutions
There are other methods to find multiple matches in a string. One method is to use the std::sregex_iterator:
<code class="cpp">int main() { std::regex r("ab(c)"); std::string s = "abcdefabcghiabc"; for (std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r); i != std::sregex_iterator(); ++i) { std::smatch m = *i; std::cout << "Match value: " << m.str() << " at Position " << m.position() << '\n'; std::cout << " Capture: " << m[1].str() << " at Position " << m.position(1) << '\n'; } return 0; }
This code will return:
Match value: abc at Position 0 Capture: c at Position 2 Match value: abc at Position 6 Capture: c at Position 8 Match value: abc at Position 12 Capture: c at Position 14
Another method is to use the std::regex_match function:
<code class="cpp">int main() { std::regex r("abc"); std::string s = "abcdefabcghiabc"; std::vector<std::string> matches; std::smatch match; while (std::regex_search(s, match, r)) { matches.emplace_back(match[0]); s = match.suffix().str(); } std::cout << matches.size() << std::endl; }</code>
This code will also return 3.
The above is the detailed content of Why does `std::match_results::size` not return the number of matches in a string?. For more information, please follow other related articles on the PHP Chinese website!