samedi 18 mars 2017

C++ regex match all words and integers

Given the following input string:

dog = cat + 1 + 283 + mouse

I want to create two regular expressions, one to capture all the words and the other to capture the numbers.

Using #include <regex> while on C++11 my regex's look like:

regex word_regex("([a-zA-Z]+)", std::regex::extended);
regex number_regex("([0-9]+)", std::regex::extended);

Now on rubular, both of those expressions capture exactly what I want but in my C++ code, which looks like:

regex word_regex("([a-zA-Z]+)", std::regex::extended);
regex number_regex("([0-9]+)", std::regex::extended);
  for(auto line: this -> data) {
    smatch word_matches;
    smatch number_matches;
    regex_search(line, word_matches, word_regex);
    regex_search(line, number_matches, number_regex);
}

where this -> data is of type vector<string>, when I print out the match groups:

for(auto x: word_matches) { cout << x << endl; }
for(auto x: number_matches) { cout << x << endl; }

the output is always the first occurrence of a match twice. For my input string that would print out ["dog", "dog"] and [1, 1]. That would lead me to believe that this is a quirk of C++ I'm unaware of.

Does anyone have any leads on how to accomplish what I want and more importantly, why what I have doesn't work? Should I be using std::regex::basic instead?

Aucun commentaire:

Enregistrer un commentaire