First, Introduction
A good programmer is a programmer who uses DB and regular expression to see how important both are. Regular expressions are tools that can greatly improve productivity, and people who have used various tools with the RE feature under Linux must be deeply impressed. Many languages support re, with the most of course scripts, with Perl being the most popular. However, in C language to use RE is not a lot of see, but sometimes very useful, I also recently saw others say this, so search for some information plus their own experience to speak of re in C language application. C language itself does not have re features, but there are many libraries, under Linux you can easily use the library provided by Regex.h.
Second, the API
http://blog.chinaunix.net/uid-479984-id-2114941.html
Http://blog.sina.com.cn/s/blog_6a1837e901010ckv.html
Http://blog.chinaunix.net/uid-17240700-id-2813921.html
Third, examples
example1.c
#include <stdio.h>#include<sys/types.h>#include<regex.h>#include<memory.h>#include<stdlib.h>intMain () {Char*bematch ="[email protected]"; Char*pattern ="h{3,10} (. *) @. {5}. (.*)"; Charerrbuf[1024x768]; Charmatch[ -]; regex_t reg; intERR,NM =Ten; regmatch_t PMATCH[NM]; if(Regcomp (®,pattern,reg_extended) <0) {Regerror (err,®,errbuf,sizeof (ERRBUF)); printf ("err:%s\n", ERRBUF); } Err= Regexec (®,bematch,nm,pmatch,0); if(Err = =Reg_nomatch) {printf ("No match\n"); Exit (-1); }Else if(Err) {Regerror (err,®,errbuf,sizeof (ERRBUF)); printf ("err:%s\n", ERRBUF); Exit (-1); } for(intI=0;i<Ten&& pmatch[i].rm_so!=-1; i++){ intLen = pmatch[i].rm_eo-Pmatch[i].rm_so; if(len) {memset (match,' /', sizeof (match)); memcpy (Match,bematch+Pmatch[i].rm_so,len); printf ("%s\n", Match); }} return0;}
Compile
gcc -g-o example1 example1.c--std=c99
Run
C language using regular expressions