vendredi 12 mars 2021

`regex_match` returns both 'not found' and `match_results`

In the following code (gcc 10.2.1), the call to regex_match returns 'no match', which I believe is correct.

sm.size() returns 0, but when iterating from sm.begin() to end(), it finds 3 occurences (all empty strings).

If this is correct, what do these 3 finds mean ?
If not correct, why isn't begin() == end() ?

#include <iostream>
#include <string>
#include <regex>
#include <assert.h>

int main()
{
    std::string input("4321");
    std::regex rg("^([0-9])");
    std::smatch sm;

    bool found = std::regex_match(input, sm, rg);

    assert(!sm.size() == sm.empty());

    std::cout << "found: " << found << ", size: " << sm.size() << std::endl;

    for (auto it = sm.begin(); it != sm.end(); ++it)
    {
        std::cout << "iterate '" << *it << "'\n";
    }
}

output:

found: 0, size: 0
iterate ''
iterate ''
iterate ''

Aucun commentaire:

Enregistrer un commentaire