samedi 14 mars 2015

C++11 regex_match giving unexpected results


I'm trying to write a recursive descent parser and am trying to search a match a regex within a string inputted by the user. I am trying to do the following to try to understand the <regex> library offered by C++11, but I'm getting unexpected results.



std::string expression = "2+2+2";
std::regex re("[-+*/()]");
std::smatch m;

std::cout << "My expression is " << expression << std::endl;

if(std::regex_search(expression, re)) {
std::cout << "Found a match!" << std::endl;
}

std::regex_match(expression, m, re);

std::cout << "matches:" << std::endl;
for (auto it = m.begin(); it!=m.end(); ++it) {
std::cout << *it << std::endl;
}


So based on my regular expression, I expect it to output



Found a match!
matches:
+
+


However, the output I get is:



My expression is 2+2+2
Found a match!
matches:


I feel like I'm making a stupid mistake, but I can't seem to figure out why there's a discrepancy between the outputs.


Thanks, erip




Aucun commentaire:

Enregistrer un commentaire