Skip to content

Instantly share code, notes, and snippets.

@mojombo
Created October 29, 2008 00:29
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 mojombo/20567 to your computer and use it in GitHub Desktop.
Save mojombo/20567 to your computer and use it in GitHub Desktop.
An example of using regex in C
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <stdio.h>
int main() {
regex_t *preg = calloc(1, sizeof(regex_t));
int numoffsets = 5;
regmatch_t pmatch[numoffsets];
int rc = regcomp(preg, "^.*(foo).(bar).*$", REG_EXTENDED);
printf("regcomp output = %i\n", rc);
int res = regexec(preg, "one foo-bar two", numoffsets, pmatch, 0);
printf("regexec output = %i\n", res);
int i;
for(i = 0; i < numoffsets; ++i) {
printf("found (%lld, %lld)\n", pmatch[i].rm_so, pmatch[i].rm_eo);
}
regfree(preg);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment