Why doesn't Boost.Regex find multiple matches on the same line?

I am writing a small program from the command line that asks the user for polynomials in the form ax ^ 2 + bx ^ 1 + cx ^ 0. I'm going to analyze the data later, but for now I'm just trying to check if I can combine the polynomial with the regular expression (\+|-|^)(\d*)x\^([0-9*]*)My problem lies in the fact that it does not correspond to several terms in the polynomial entered by the user, if only I change it to ((\+|-|^)(\d*)x\^([0-9*]*))*(the difference is that the whole expression is grouped and an asterisk at the end). The first expression works if I type something like " 4x^2" but not " 4x^2+3x^1+2x^0", as it does not check several times.

My question is why Boost.Regex regex_match()will not find multiple matches within the same line? It is used in the regular expression editor that I used (Expresso), but not in C ++ code. Is this supposed to be so?

Let me know if something doesn't make sense and I will try to clarify. Thanks for the help.

Edit1: Here is my code (I follow the tutorial here: http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3 )

int main()
{
    string polynomial;

    cmatch matches; // matches

    regex re("((\\+|-|^)(\\d*)x\\^([0-9*]*))*");

    cout << "Please enter your polynomials in the form ax^2+bx^1+cx^0." << endl;

    cout << "Polynomial:";
    getline(cin, polynomial);

    if(regex_match(polynomial.c_str(), matches, re))
    {
        for(int i = 0; i < matches.size(); i++)
        {
            string match(matches[i].first, matches[i].second);
            cout << "\tmatches[" << i << "] = " << match << endl;
        }
    }

    system("PAUSE");
    return 0;
}
+5
source share
1 answer

- regex_match , () . , , , - . , , , ( Kleene). , , , - regex_token_iterator.

Edit: , ++, . , , - , . , , , "+", "-" "^", . , "^". , , , . , - : "[- +]? (\ D *) x (\ ^ ([0-9]) *)".

, - :

#include <iterator>
#include <regex>
#include <string>
#include <iostream>

int main() { 

    std::string poly = "4x^2+3x^1+2x";

    std::tr1::regex term("[-+]?(\\d*)x(\\^[0-9])*");

    std::copy(std::tr1::sregex_token_iterator(poly.begin(), poly.end(), term),
        std::tr1::sregex_token_iterator(), 
        std::ostream_iterator<std::string>(std::cout, "\n"));
    return 0;
}

, :

4x ^ 2
+ 3x ^ 1
+ 2

, , , (, ).

: std::cout, - :

#include <iterator>
#include <regex>
#include <string>
#include <iostream>

int main() {   
    std::string poly = "4x^2+3x^1+2x";

    std::tr1::regex term("[-+]?(\\d*)x(\\^[0-9])*");
    std::vector<std::string> terms;

    std::copy(std::tr1::sregex_token_iterator(poly.begin(), poly.end(), term),
        std::tr1::sregex_token_iterator(), 
        std::back_inserter(terms));

    // Now terms[0] is the first term, terms[1] the second, and so on.

    return 0;
}
+7

All Articles