Hello. There is the following example:

#include <iostream> #include <regex> int main() { std::string str("subject, subbase"); std::regex rx("sub\\w*"); std::smatch res; std::regex_search(str, res, rx); for(auto &i: res) { std::cout << i << " "; } return 0; } 

Output: subject

I was expecting the output of both words from str , and then both words highlight me. G ++ (GCC) 6.1.1. Maybe I did not understand correctly the principle of the function regex_search? It should make all the matches in res, everything seems to be correct.

  • In short, the most interesting was, but the answer, essno googled by request regex_search multiple results c ++, and is apparently located at stackoverflow.com/questions/21667295/… - strangeqargo
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • This question concerns exclusively c ++ 11 ? - Nick Volynkin

1 answer 1

Cycle

 for(auto &i: res) std::cout << i << " "; 

passes by captured groups as a result of the search.
If your regular season to write, for example, in this form:

 std::regex rx("(sub)(\\w*)"); 

then the following groups will be captured:

subject sub ject

To match all the words that match your regular season, you can use the code:

 while (std::regex_search (str, res, rx)) { std::cout << res[0] << " "; str = res.suffix(); } 

An example of using regex_search can be found, for example, here .