导读:gnu的grep和fgrep程序内部都调用了GLibc的4个函数实现正则表达式的搜索,他们是:
int regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
int regexec (const regex_t *__restrict preg, const char *__restrict string,
size_t nmatch, regmatch_t pmatch[], int eflags)
size_t regerror (int errcode, const regex_t * preg, char *errbuf,
size_t errbuf_size)
void regfree (regex_t *preg)
这4个函数也是POSIX规范里要求的4个函数。GNU还实现了re_match函数和re_search函数,功能类似。
正则表达式中使用了2个重要的概念DFA和NFA(至今还不理解。。。)
下面的例子展示了这4个函数的使用方法:
#include "types.h"
#include "mcheck.h"
#include "regex.h"
#include "stdio.h"
#include "stdlib.h"
#include"regex_internal.h"
static const char text[] = "This is a test; this is a test";
int main (void)
{