Regex in C language using regcomp and regexec

From Code Trash
Jump to: navigation, search
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <regex.h>
#include <conio.h>
#include <stdio.h>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    int a;
    regex_t re;
    char str[128] = "once sam lived with samle to win samile";
    regmatch_t pm;
    size_t errn;
 
    char errbuf[128];
    errbuf[0] = 0;
 
    a = regcomp(&re,"sam", REG_EXTENDED);
    if(a!=0)
    {
        puts("Invalid Regex");
        getch();
        return 0;
    }
    printf("%d",a);
 
    a = regexec(&re, &str[0], 1, &pm, REG_EXTENDED);
    printf("%d",a);
 
    regerror(a, &re, errbuf, 128);    
    puts(errbuf);
 
    int cnt = 0;
 
    while(a==0)
    {
        cnt += pm.rm_eo;
 
        a = regexec(&re, &str[0] + cnt, 1, &pm, 0);
 
        printf("\n next pos %d",cnt);        
    }
 
    getch();  
    return EXIT_SUCCESS;
}

For my reference

Linker="../../../../Program Files/Dev-Cpp/lib/libregex.dll.a"_@@_regex.lib_@@_

The project folder contained regex.lib which i have added using project properties

file libregex.dll.a is copied to the lib folder

and regex3.dll was copied to the windows system32 folder

have to check whether regex.lib needs compilation

the ide used is wxDev-C++ build 7.3.1.3

Reference

You can find libraries and documentation in the following url

Download C Regex Library

And refer this question

http://stackoverflow.com/questions/11377631/regex-in-c-language-using-functions-regcomp-and-regexec-toggles-between-first-an