summaryrefslogtreecommitdiff
path: root/pcre/simple.c (plain)
blob: e0a38e79185171edb16ed1a3889e141257d9ee6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <pcre.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int i,rc;

  /* for pcre to report compilation errors back to us */
  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);
    }
  }
}