1

I use pcrecpp c++ ( PCRE lib )
And i need get all matches in cycle. How can i do it?

For example pattern:
"hello"

and subject:
"hello hello hello"

Cycle should loop 3 times ( because 3 matches )
1 hello
2 hello
3 hello

Pseudocode

pcrecpp::RE pPattern ( "hello" );  
std::string strBase = "hello hello hello";  
// ...  
int iMatches = // Match count 
for ( int i = 1; i < iMatches; i++ )   
{  
    printf( "%d %s", i, pPattern[ i ].c_str () );  
}

Please give me some example code how to do it with pcrecpp.h.
Sorry for my bad english.

1 Answers1

4

Even though this question is already some months old, I'll provide a solution here:

#include <pcrecpp.h>
#include <iostream>

int main(void)
{
  pcrecpp::RE regex("(hello)");  
  std::string strBase = "hello hello hello";  

  pcrecpp::StringPiece input(strBase);

  std::string match;

  int count = 0;
  while (regex.FindAndConsume(&input, &match)) {
    count++;
    std::cout << count << " " << match << std::endl;
  }
}

For more information, this site might help.

lslah
  • 556
  • 8
  • 17