Created
October 29, 2008 00:29
-
-
Save mojombo/20567 to your computer and use it in GitHub Desktop.
An example of using regex in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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