#include #include #include #define VECTORSIZE 30 // compile: gcc -o pcretest -lpcre pcretest.c int main(void) { // Incoming HTML char *myhtml = "
  • google
  • is
  • a
  • search
  • engine<"; // Pointer to HTML char *ptr = myhtml; // Configure regex pattern pcre *re; const char *error; int erroroffset; re = pcre_compile("
  • (.+?)<", PCRE_CASELESS|PCRE_DOTALL, &error, &erroroffset, NULL); // Using a pointer starting at the front of the string, try and find a single match // when a match is found, move that pointer past the match and look for another one // keep doing that until you run out of matches while (ptr!=NULL) { // do the actual regex match int rc; int ovector[VECTORSIZE]; rc = pcre_exec(re, NULL, ptr, strlen(ptr), 0, 0, ovector, VECTORSIZE); // check if there was a match if (rc>0) { // print out the match int i = 1; char *temp = calloc(ovector[i*2+1]-ovector[i*2]+1, sizeof(char)); strncpy(temp, ptr+ovector[i*2], ovector[i*2+1]-ovector[i*2]); printf("found = %s\n", temp); free(temp); // move the pointer past the match ptr = ptr+ovector[i*2]; } else { // no match = give up ptr = NULL; } } }