Last active
August 29, 2015 14:14
-
-
Save rinevo/7361a796970a6c67c2b4 to your computer and use it in GitHub Desktop.
正規表現で文字列の抽出
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
# Makefile | |
regex: regex.c | |
gcc -o regex regex.c | |
clean: | |
rm -f regex |
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
// 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(®ex, reg, REG_EXTENDED | REG_NEWLINE); | |
if (ret) { | |
regerror(ret, ®ex, 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(®ex, target, nmatch, match, 0); | |
if (ret) { | |
regerror(ret, ®ex, buf, sizeof(buf)); | |
printf("Error: regexec() %s\n", buf); | |
regfree(®ex); | |
exit( EXIT_FAILURE ); | |
} | |
printf("target: %s\n", target); | |
// 結果出力 | |
int nmatch_last = nmatch - 1; | |
regerror(ret, ®ex, 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(®ex); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment