Thread: Question about regular expression

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Unhappy Question about regular expression

    Hi all,

    I'm new to C program, and I have some coding problems which is about regex.h.

    I'm trying to find the matching pattern "-w[0-9]*" in the string. But I just couldn't figure out
    what should I put in regcomp brackets.

    Can someone please tell me?

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    regcomp function is used to compile a regular expression into a form to be used by the subsequent regexec function.

    Actually,regexec function only used to match the pattern.

    If you want to match the -w[0-9]* string.Then,you use the following program.
    Code:
    #include <regex.h>  //including the functions for matching regular expressions                                                                                                                
    #include <stdio.h>   //including the standard functions                                                           
    #include <stdlib.h>  //including the standard library functions                                                           
                                                                                    
    main() {                                                                        
        regex_t    preg;    //creating the                                                         
        char       *string = "-w1";                                     
        char       *pattern = "-w[0-9]*";                                       
        int        rc;                                                              
        size_t     nmatch = 2;   //nmatch and pmatch are used  to provide informations about the matches                                                  
        regmatch_t pmatch[2];                                                       
                                                                                    
        if ((rc = regcomp(&preg, pattern, REG_EXTENDED)) != 0) {  //giving the pattern string                    to regcomp function to make the string pattern in the form to match by regexec function 
           printf("regcomp() failed, returning nonzero (%d)\n", rc);                
           //exit(1);                                                                 
        }                                                                           
                                                                                    
        if ((rc = regexec(&preg, string, nmatch, pmatch, 0)) != 0) {   //matching the regular expression             
           printf("failed to ERE match '%s' with '%s',returning %d.\n",             
           string, pattern, rc);                                                    
        }                                                                           
        // Both the functions on success,returns 0.
        else if(rc==0)
    	    printf ( "Pattern Matched\n" );
        regfree(&preg);                                                             
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Code:
            regex_t re;
            char patt[]="-w[0-9]*";
            char string[]="-w100";
            int status;
            regcomp(&re,patt,REG_EXTENDED);
            status=regexec(&re,string,(size_t)0,NULL,0);
    regexec() will return zero for successful match.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    ty.. Im trying to figure out how do those functions work =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regular expression for matching XML
    By bling in forum Tech Board
    Replies: 4
    Last Post: 11-04-2008, 09:26 PM
  2. Regular Expression (Pattern Matching)
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2008, 09:04 AM
  3. regular expression searches
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 08-11-2008, 06:49 PM
  4. Expression Tree
    By Coding in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2008, 04:24 PM
  5. Regular Expression
    By stevesmithx in forum C Programming
    Replies: 0
    Last Post: 02-18-2008, 11:00 AM