GLibc中regcomp搜索正则表达式的实现分析, search algorithm of regular expression on glibc

本文深入分析了GLibc中用于正则表达式搜索的regcomp函数,该函数遵循POSIX规范,并通过re_compile_internal构造DFA和二叉树,进行模式匹配。文中提及DFA和NFA的概念,但未详细展开。
摘要由CSDN通过智能技术生成

导读: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)
{
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值