Skip to content

Instantly share code, notes, and snippets.

@ambethia
Created February 23, 2010 00:34
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 ambethia/311712 to your computer and use it in GitHub Desktop.
Save ambethia/311712 to your computer and use it in GitHub Desktop.
Regex example in c
#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(&regex, pattern, REG_EXTENDED);
if ((regexec(&regex, 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(&regex);
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