All pastes #244659 Raw Edit

Something

public text v1 · immutable
#244659 ·published 2006-11-10 01:00 UTC
rendered paste body
#include <stdio.h>
#include <pcre/pcre.h>
#include <string.h>

#define VECTORSIZE 30

// compile: gcc -o pcretest -lpcre pcretest.c

int main(void)
{

// Incoming HTML
char *myhtml = "<li>google<li>is<li>a<li>search<li>engine<";

// Pointer to HTML
char *ptr = myhtml;


// Configure regex pattern
pcre *re;
const char *error;
int erroroffset;
re = pcre_compile("<li>(.+?)<", 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;
	}
}
}