Home > Backend Development > C++ > Why does `std::match_results::size` not return the number of matches in a string?

Why does `std::match_results::size` not return the number of matches in a string?

Barbara Streisand
Release: 2024-11-07 22:16:02
Original
858 people have browsed it

Why does `std::match_results::size` not return the number of matches in a string?

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template