Skip to content

Instantly share code, notes, and snippets.

@thesecretmaster
Last active December 10, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesecretmaster/e2293fb83cc9cb63433bd05bce7dbffb to your computer and use it in GitHub Desktop.
Save thesecretmaster/e2293fb83cc9cb63433bd05bce7dbffb to your computer and use it in GitHub Desktop.
#include <pcre.h>
#define OVECCOUNT 90
pcre *SimpleCompileRegex(const char *pattern, char ** errmsg) {
const char *error;
int erroffset;
pcre *re = pcre_compile(
pattern, /* the pattern */
0, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL);
if (re == NULL) {
char *buf;
size_t sz;
sz = snprintf(NULL, 0, "PCRE compilation failed at offset %d: %s", erroffset, error);
buf = (char *)malloc(sz + 1); /* make sure you check for != NULL in real code */
snprintf(buf, sz+1, "PCRE compilation failed at offset %d: %s", erroffset, error);
*errmsg = buf;
}
return re;
}
int CheckCompiledRegexOnString(pcre *regex, const char *str) {
int subject_length = (int)strlen(str);
int ovector[OVECCOUNT];
int rc = pcre_exec(
regex, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
str, /* the subject string */
subject_length, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
// free(subject);
// pcre_free(regex); /* Release memory used for the compiled pattern */
if (rc < 0)
{
// Some dtails about why it didn't match. See "Error return values from pcre_exec() in man pcreapi" (or line 1832)
// switch(rc)
// {
// case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
// /*
// Handle other special cases if you like
// */
// default: printf("Matching error %d\n", rc); break;
// }
return rc;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment