[LCP]sample code for matching regexp

prashanth mukundan mukundanprashanth at hotmail.com
Fri Sep 14 01:35:15 UTC 2001


hi

    this code pattern matches using PCRE- Perl Compatible Regular 
Expressions.the tutorial for these functions is also available on the net.i 
think google should help u find it. to work on this u might require to 
download pcre.h header.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pcre.h>

int main(void)
{
	static pcre * reg;
        static pcre_extra * studiedreg;
   	const char *comperr;
        const char *studiederr;
        int erroff;
        //char patt[]="(<a href=\"/r.*<a 
href=\"http://jump.altavista.com/trans)|(<p><a href=.*<a 
href=/search?q=cache)";
        //char string[]= "<dl><dt><b class=txt2>1.</b><b><a 
href=\"/r?r=http://volcanoes.usgs.gov/\"onMouseOver=\"status='http://volcanoes.usgs.gov/'; 
return true;\"> U.S.Geological Survey, Volcano Hazards<i>Program</i></a></b> 
<dd>A comprehensive overview of the U.S. Geological Survey's Volcano Hazards 
<b>Program</b> and current volcanic activity in the United States. The 
Volcano...<br><span class=ft>URL: http://volcanoes.usgs.gov/</span><br><a 
href=\"http://jump.altavista.com/trans.go?urltext=http://volcanoes.usgs.gov/&language=en\">Translate</a>&nbsp; 
<a 
href=\"http://jump.altavista.com/more_from_site.go?q&stype=stext&Translate=on& 
s c=on&q=program&si=3a85253b068ccf49\">More pages from this site</a>&nbsp; 
</dl> <p><A HREF=http://www.usgcrp.gov/>USGCRP Home</A><font size=-1><br> 
<b>...</b> version, 321 kb). A report (dtd December 2000) to the US Global 
Change Research<br><b>Program</b> from an ad hoc Working Group on Climate 
Modeling. (link posted 18 Jan <b>...</b><br><font size=-1 
color=#6f6f6f>Description:</font>Scientific research, links, educational 
resources, US national assessment of the potential consequences... <br><font 
size=-1 color=#6f6f6f>Category:</font><a 
href=http://directory.google.com/Top/Science/Environment/Global_Change/ 
class=l>Science&nbsp;&gt;&nbsp;Environment&nbsp;&gt;&nbsp;Global&nbsp;Change</a><br><font 
color=green>www.usgcrp.gov/ - 7k -<A 
HREF=/search?q=cache:www.usgcrp.gov/+program&hl=en class=l>Cached</A> -<A 
HREF=/search?hl=en&lr=&safe=off&num=10&q=related:www.usgcrp.gov/ 
class=l>Similar pages</A></font></font><br>" ;
	char string[]="abcfgjfjajnhfdbghsdcgtastaababcxhjjhd";
        char patt[]="^abc";

	const char *mallocptr ;
	int n,j,strlength,length;

        int  offset[99];
	char buffer[100];
        char *buf=string;
	reg=pcre_compile(patt,PCRE_CASELESS|PCRE_MULTILINE,&comperr,&erroff,NULL);
	if(reg==NULL)
	{
		fprintf(stderr,"error while compiling%s\n",comperr);
	}
	studiedreg=pcre_study(reg,0,&studiederr);
        if(studiederr!=NULL)
        {
           fprintf(stderr,"error while studying %s\n",studiederr);
        }

        /*i=pcre_fullinfo(reg,studiederr,PCRE_INFO_CAPTURECOUNT,count);
        if(i<0)
        {
             printf("error while getting info");
        }
        ovecsize=*count*3;
        */

        for(j=0;j<15;j++)
        {
        length=(int)strlen(string);
	n=pcre_exec(reg,studiedreg,buf,length,0,0,offset,100);
        if(n==PCRE_ERROR_NOMATCH)
        {
            fprintf(stderr,"error while matching");
        }

        //printf("the numfer of submatches %d",n);


strlength=pcre_get_substring(buf,offset,n,0,&mallocptr);
        if(strlength<0)
        {
             printf("error while string extraction %d",strlength);
        }

        printf("the string is : %s\n",mallocptr);

        buf+=offset[3];
        }

}

with luv,
prashanth mukundan

>From: Biki <biki at deeproot.co.in>
>Reply-To: linuxcprogramming at lists.linux.org.au
>To: LinuxCProgramming Mailing list <linuxcprogramming at lists.linux.org.au>
>Subject: [LCP]sample code for matching regexp
>Date: Mon, 10 Sep 2001 18:28:46 +0530 (IST)
>
>
>
>
>Hello Everybody
>
>I am trying to use the GNU C library
>for matching regular epressions.
>I have been refering to the man pages of help.
>But i would like to go thru some sample
>codes where regular expression matching functions
>have been used .
>I haven't been able to find any yet.
>Any help in this regard will be
>appreciated.
>
>Cheers
>
>biki
>
>
>
>
>_______________________________________________
>This is the Linux C Programming List
>:  http://lists.linux.org.au/listinfo/linuxcprogramming List


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




More information about the linuxCprogramming mailing list