Skip to content

Instantly share code, notes, and snippets.

@rinevo
Last active August 29, 2015 14:14
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 rinevo/7361a796970a6c67c2b4 to your computer and use it in GitHub Desktop.
Save rinevo/7361a796970a6c67c2b4 to your computer and use it in GitHub Desktop.
正規表現で文字列の抽出
# Makefile
regex: regex.c
gcc -o regex regex.c
clean:
rm -f regex
// regex.c
// 正規表現で文字列の抽出
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
int main(int argc, char *argv[])
{
int ret = 0;
char buf[BUFSIZ];
// 正規表現パターンをコンパイル
char *reg = "<[aA]>(.*?)</[aA]>"; // 正規表現
regex_t regex;
ret = regcomp(&regex, reg, REG_EXTENDED | REG_NEWLINE);
if (ret) {
regerror(ret, &regex, buf, sizeof(buf));
printf("Error: regcomp() %s\n", buf);
exit(EXIT_FAILURE);
}
printf("reg: %s\n", reg);
// コンパイラが認識した部分正規表現の数だけマッチング結果を格納する領域の確保
int nmatch = regex.re_nsub + 1;
regmatch_t *match = (regmatch_t*)malloc(sizeof(regmatch_t) * nmatch);
printf("nmatch: %d\n", nmatch);
// パターンマッチング
const char *target = "<a>hogehoge</a>"; // 検索する文字列
ret = regexec(&regex, target, nmatch, match, 0);
if (ret) {
regerror(ret, &regex, buf, sizeof(buf));
printf("Error: regexec() %s\n", buf);
regfree(&regex);
exit( EXIT_FAILURE );
}
printf("target: %s\n", target);
// 結果出力
int nmatch_last = nmatch - 1;
regerror(ret, &regex, buf, sizeof(buf));
int len = match[nmatch_last].rm_eo - match[nmatch_last].rm_so;
printf("result: %s(len=%d)\n", buf, len);
if (len > 0) {
strncpy(buf, &target[match[nmatch_last].rm_so], len);
buf[len] = 0;
printf("match: %s\n", buf);
}
// コンパイルした正規表現のバッファを開放
regfree(&regex);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment