C language using regular expressions

Source: Internet
Author: User

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 (&reg,pattern,reg_extended) <0) {Regerror (err,&reg,errbuf,sizeof (ERRBUF)); printf ("err:%s\n", ERRBUF); } Err= Regexec (&reg,bematch,nm,pmatch,0); if(Err = =Reg_nomatch) {printf ("No match\n"); Exit (-1); }Else if(Err) {Regerror (err,&reg,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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.