Examples

The following example compiles an globalized regular expression and matches a string using this compiled expression. A match is found for the first pattern, but no match is found for the second pattern.

#include                 <locale.h>
#include                 <regex.h>
 
#define                  BUFSIZE    256
 
 
main()
{
        char    *p;
 
        char    *pattern[] = {
                "hello[0-9]*",
                "1234"
        };
        
 
        char     *string = "this is a test string hello112 and this is test";
                /* This is the source string for matching */
 
        int     retval;
        regex_t   re;
        char    buf[BUFSIZE];
 
        int     i;
 
        setlocale(LC_ALL, "");
 
        for(i = 0;i <2; i++){
                retval = match(string, pattern[i], &re);
                if(retval == 0){
                        printf("Match found \n");
                }else{
                        regerror(retval, &re, buf, BUFSIZE);
                        printf("error = %s\n", buf);
                }
        }
        regfree( &re);
}
 
 
int match(char *string, char *pattern, regex_t *re)
{
        int     status;
 
        if((status=regcomp( re, pattern, REG_EXTENDED))!= 0)
                return(status);
        status = regexec( re, string, 0, NULL, 0);
        return(status);
}

The following example finds all substrings in a line that match a pattern. The numbers 11 and 2001 are matched. Every digit that is matched counts as one match. There are six such matches corresponding to the six digits supplied in the string.

#include         <locale.h>
#include                <regex.h>
 
#define                BUFSIZE   256
 
 
main()
{
        char    *p;
 
        char    *pattern = "[0-9]";
        char    *string = "Today is 11 Feb 2001 ";
 
        int     retval;
        regex_t re;
        char    buf[BUFSIZE];
        regmatch_t pmatch[100];
        int      status;
        char     *ps;
 
        int      eflag;
 
        setlocale(LC_ALL, "");
 
        /* Compile the pattern */
 
        if((status = regcomp( &re, pattern, REG_EXTENDED))!= 0){
                regerror(status, &re, buf, 120);
                exit(2);
        }
 
        ps = string;
        printf("String to match=%s\n", ps);
        eflag = 0;
 
        /* extract all the matches */
        while( status = regexec( &re, ps, 1, pmatch, eflag)== 0){
                printf("match found at: %d, string=%s\n", 
                        pmatch[0].rm_so, ps +pmatch[0].rm_so);
                ps += pmatch[0].rm_eo;
                printf("\nNEXTString to match=%s\n", ps);
                eflag = REG_NOTBOL;
 
        }
        regfree( &re);
}

The following example uses the fnmatch subroutine to read a directory and match file names with a pattern.

#include                 <locale.h>
#include                 <fnmatch.h>
#include                 <sys/dir.h>
 
main(int argc, char *argv[] )
{
        char    *pattern;
        DIR     *dir;
        struct dirent    *entry;
        int     ret;
 
 
                setlocale(LC_ALL, "");
 
        dir = opendir(".");
        
        pattern = argv[1];
 
        if(dir != NULL){
                while( (entry = readdir(dir)) != NULL){
                        ret = fnmatch(pattern, entry->d_name, 
                                FNM_PATHNAME|FNM_PERIOD);
                        if(ret == 0){
                                printf("%s\n", entry->d_name);
                        }else if(ret == FNM_NOMATCH){
                                continue ;
                        }else{
                                printf("error file=%s\n", 
                                        entry->d_name);
                        }
                }
                closedir(dir);
        }
}