PCREを使ったCプログラミング(1)

[id:lethevert:20051211:p4]
連載化!

      • -

まずは、シンプルなサンプルプログラム。(まだ、細かいところはよく分かっていない)

#include <stdio.h>
#include <assert.h>
#include <pcre.h>

int main(int argc, char** argv)
{
	const char* regex = "aa";
	const char* err_msg;
	int err_offset;
	pcre* p = pcre_compile
		(regex, 0, &err_msg, &err_offset, NULL);
	assert(p != NULL);

	const char* test = "aaabbbaa";
	const int len = 9;
	int ovector[3];
	int status = pcre_exec
		(p, NULL, test, len, 0, 0, ovector, 3);
	if (status > 0) {
		printf("%s\n", test);
	}
	return 0;
}

コンパイルは、このように。

$ gcc main.c `pcre-config --libs`

pcre-configというのは、pcreを使うのに、ちょっとだけ便利なコマンドという感じのもの。