Std :: regex equivalent to the global modifier '/ g'

In Perl, I can do this:

$text = '1747239';
@matches = ($text =~ m/(\d)/g);
# @matches now contains ('1', '7', '4', '7', '2', '3', '9')

Using C ++ regular expression matching, the best way to reproduce this behavior is that I get a set of matches, including all matches?

I have this at the moment: -

compiledRegex = std::regex(regex, std::tr1::regex_constants::extended);
regex_search(text, results, compiledRegex);

int count = results.size();
// Alloc pointer array based on count * sizeof(mystruct).
for ( std::cmatch::iterator match = results.begin(); 
      match != results.end(); 
      ++match )
{
    // Do something with match;
}

However, this will only give me the first match, just like Perl without / g, which is good, but I need the / g effect.

So, is there a good way to do this, or should I continue to work with regex over and over?

+5
source share
1 answer

regex_search . , . , , . , , , . , count == 1

std::string::const_iterator text_iter = text.cbegin();
compiledRegex = std::regex(regex, std::tr1::regex_constants::extended);

while (regex_search(text_iter, text.end(), results, compiledRegex))
{
    int count = results.size();
    // Alloc pointer array based on count * sizeof(mystruct).
    for ( std::cmatch::iterator group = results.begin();
          group != results.end();
          ++group )
    {
        // If you uses grouping in your search here you can access each group
    }

   std::cout << std::string(results[0].first, results[0].second) << endl;
   text_iter = results[0].second;
}

,

+8

All Articles