libre compiles regular expressions into NFAs for reasonably fast matching speeds. Its interface is very simple; it exposes three functions, all of which are self-explanatory.
re_pat *re_compile(char const *)
bool re_match(re_pat const *, char const *, struct re_result *)
void re_free(re_pat *)
To use libre, simply clone this repository and run make && sudo make install
.
#include <re.h>
#include <stdio.h>
#include <assert.h>
int main(void)
{
re_pat *p = re_compile("(https?://)?www\\.google\\.(ca|com)");
if (p == NULL) {
fputs("Failed to compile regular expression.\n", stderr);
return 1;
}
assert(re_match(p, "https://www.google.ca", NULL));
assert(re_match(p, "http://www.google.ca", NULL));
assert(re_match(p, "https://www.google.com", NULL));
assert(re_match(p, "https://www.google.com", NULL));
assert(re_match(p, "www.google.ca", NULL));
assert(re_match(p, "www.google.com", NULL));
assert(!re_match(p, "www.github.com", NULL));
assert(!re_match(p, "http://google.com", NULL));
assert(!re_match(p, "http://google.ca", NULL));
re_free(p);
return 0;
}
$ cc -std=c11 -o example example.c -lre
$ ./example
$