Thread: regex searching problem -> i cant get more than one result

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    regex searching problem -> i cant get more than one result

    hi everyone,

    im searching a string using regex and it works fine...but it only returns me the first result...im trying to store all the results(indicies) in an array, and return it..

    thanks for any help

    Code:
    int* search(char *file, char *currentSearch, char *tagType) {
    	
    	int error = 0;
        regex_t re;
        size_t a = 2;
        regmatch_t arrayOfMatches[1000];
    	int counter=0;
    	int *Matches;
        
        if (regcomp(&re, currentSearch, REG_EXTENDED|REG_ICASE) != 0) {
    
            return(0);      
    
        }
    
    	if ((Matches = malloc(sizeof(int))) == NULL){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for current search\n", sizeof(int));
    			exit(1);
    	}
      
    	/* Finds the matches on the line */
    	error = regexec (&re, file, a, arrayOfMatches, 0);
    
    	/* while matches found */
    	while (error == 0) {    
    	 	
    		if(strcmp(tagType,"start")){
    			Matches[counter] = arrayOfMatches[counter].rm_so;
    		}
    		
    		else{
    			Matches[counter] = arrayOfMatches[counter].rm_eo;
    		}
    		
    		if ((Matches = realloc(Matches, sizeof(int) * sizeof(Matches))) == NULL){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for file\n", sizeof(int));
    			exit(1);
    		}
    			
      	  	/* This call to regexec() finds the next match */
        	error = regexec (&re, file + arrayOfMatches[counter].rm_eo, counter, arrayOfMatches, REG_NOTBOL);
        	
    		counter++;
        }
    	
    	regfree(&re);    
        return Matches;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    Well your realloc needs work
    Code:
    void *temp = realloc( Matches, (counter+1) * sizeof *Matches );
    if ( temp != NULL ) Matches = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    k... i fixed that...still doesnt work..any other suggestions?

    thanks..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since you haven't posted your latest attempt, other than saying "I fixed it", and since you have only given us a fragment to work with, I'd suggest you add some printf statements in there so you can make sure you're getting what you think you're getting.

    If you still have problems, try testing the function in a seperat program with some sample data to make sure it works correctly. If that function works correctly, test the next function it calls or the like.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    Perhaps if you provided us with a test line and a search RE to match against it, we could test your code more fully.

    Oh, and your use of strcmp() results in an inverted test
    if(strcmp(tagType,"start"))
    You probably meant
    if(strcmp(tagType,"start")==0){
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. File Searching Problem
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 01-18-2003, 11:52 PM
  3. searching problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 12-18-2002, 09:10 PM
  4. problem in sorting and searching
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 07:47 PM
  5. Not getting good result searching an array of string.
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2002, 11:24 AM