Skip to content

Latest commit

 

History

History
59 lines (43 loc) · 1.36 KB

README.md

File metadata and controls

59 lines (43 loc) · 1.36 KB

libre

libre is a tiny regular expression library for C

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 *)

Usage

To use libre, simply clone this repository and run make && sudo make install.

Example

#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
$