#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30
int main(int argc, char **argv)
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
if (argc != 3)
{
printf("Two arguments required: a regex and a subject string\n");
return 1;
}
re = pcre_compile(
argv[1],
0,
&error,
&erroffset,
NULL);
if (re == NULL)
{
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
rc = pcre_exec(
re,
NULL,
argv[2],
(int)strlen(argv[2]),
0,
0,
ovector,
OVECCOUNT);
if (rc < 0)
{
switch(rc)
{
case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
default: printf("Matching error %d\n", rc); break;
}
return 1;
}
printf("Match succeeded\n");
if (rc == 0)
{
rc = OVECCOUNT/3;
printf("ovector only has room for %d captured substrings\n", rc - 1);
}
for (i = 0; i < rc; i++)
{
char *substring_start = argv[2] + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}
return 0;
}