Skip to content

Instantly share code, notes, and snippets.

@wmealing
Created May 25, 2013 02:33
Show Gist options
  • Save wmealing/5647692 to your computer and use it in GitHub Desktop.
Save wmealing/5647692 to your computer and use it in GitHub Desktop.
#include <pcre.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i,rc;
const char *err;
int off;
char *regex = "hello,?\\sworld!?";
pcre *re = pcre_compile(regex, 0, &err, &off, NULL);
if (re == NULL) {
printf("error %s in pattern %s at offset %u\n", err, regex, off);
exit(-1);
}
char *tests[] = { "hello, world!", "hello world!", "hello world" };
/* now we coule loop over some input and test the regex */
for(i=0; i < sizeof(tests)/sizeof(*tests); i++) {
rc = pcre_exec(re, NULL, tests[i], strlen(tests[i]), 0, 0, NULL, 0);
if (rc >= 0) {
printf("%s matches %s\n", tests[i], regex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment