Reputation:
I'm writing a small command-line program that asks the user for polynomials in the form ax^2+bx^1+cx^0. I'm going to parse the data later but for now I'm just trying to see if I can match the polynomial with the regular expression(\+|-|^)(\d*)x\^([0-9*]*)
My problem is, it doesn't match multiple terms in the user-entered polynomial unless I change it to((\+|-|^)(\d*)x\^([0-9*]*))*
(the difference is the entire expression is grouped and has an asterisk at the end). The first expression works if I type something such as "4x^2
" but not "4x^2+3x^1+2x^0
", since it doesn't check multiple times.
My question is, why won't Boost.Regex'sregex_match()
find multiple matches within the same string? It does in the regular expression editor I used (Expresso) but not in the actual C++ code. Is it supposed to be like that?
Let me know if something doesn't make sense and I'll try to clarify. Thanks for the help.
Edit1: Here's my code (I'm following 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;
}
Upvotes: 5
Views: 2807
Reputation: 490168
You're using the wrong thing -- regex_match
is intended to check whether a (single) regex matches the entirety of a sequence of characters. As such, you need to either specify a regex that matches the whole input, or use something else. For your situation, it probably makes the most sense to just modify the regex as you've already done (group it and add a Kleene star). If you wanted to iterate over the individual terms of the polynomial, you'd probably want to use something like a regex_token_iterator
.
Edit: Of course, since you're embedding this into C++, you also have to double all your backslashes. Looking at it, I'm also a little confused about the regex you're using -- it doesn't look to me like it should really work quite right. Just for example, it seems to require a "+", "-" or "^" at the beginning of a term, but the first term won't normally have that. I'm also somewhat uncertain why there would be a "^" at the beginning of a term. Since the exponent is normally omitted when it's zero, it's probably better to allow it to be omitted. Taking those into account, I get something like: "[-+]?(\d*)x(\^([0-9])*)".
Incorporating that into some code, we can get something like this:
#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;
}
At least for me, that prints out each term individually:
4x^2
+3x^1
+2x
Note that for the moment, I've just printed out each complete term, and modified your input to show off the ability to recognize a term that doesn't include a power (explicitly, anyway).
Edit: to collect the results into a vector instead of sending them to std::cout
, you'd do something like this:
#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;
}
Upvotes: 7