Created
February 23, 2010 00:34
-
-
Save ambethia/311712 to your computer and use it in GitHub Desktop.
Regex example 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 <stdio.h> | |
#include <stdlib.h> | |
#include <regex.h> | |
char *extract_value(char *string, char *pattern) | |
{ | |
int begin, end, length, i, w=0; | |
char *word = NULL; | |
regex_t regex; | |
regmatch_t match[2]; | |
regcomp(®ex, pattern, REG_EXTENDED); | |
if ((regexec(®ex, string, 2, match, 0)) == 0) | |
{ | |
begin = (int)match[1].rm_so; | |
end = (int)match[1].rm_eo; | |
length = end - begin; | |
word = malloc(length+1); | |
for (i = begin; i < end; i++) | |
{ | |
word[w] = string[i]; | |
w++; | |
} | |
word[w] = 0; | |
} | |
regfree(®ex); | |
return word; | |
} | |
int main() { | |
char *match = extract_value("xoffset=0 yoffset=-4 xadvance=5", "yoffset=([-0-9]+)"); | |
printf("%s\n", match); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment