Thread: PCRE string issue

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    15

    PCRE string issue

    I am working on PCRE for the first time . my code needs to find the @ refernce as u would see in twitter and remove it. knowing that pcre library has no replace function . i wrote myself . it works fine when i use the function once . but iterating again corrupts my string .
    Any idea

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <regex.h>
    #include <pcre.h>
    
    
    char* sanitize(char*);
    char* stringReplace(char* subj,int start,int end,char* replace);
    char* pcreReplace(char* regex,char* txt);
    
    
    int main()
    {
        char* str="a ina @abcds http://aa.com @abcds ";
        str=sanitize(str);
        //printf("%s\n",str);
        return 0;
    }
    
    
    char* sanitize(char* str)
    {
        //remove @ reference
        str=pcreReplace("@([^\\s]){1,}",str);
        str=pcreReplace("@([^\\s]){1,}",str);
        //str=pcreReplace("https?([^\\s]){1,}",str);
        
        return str;
    }
    
    
    char* pcreReplace(char* re,char* txt)
    {
        const char *error;
        int erroffset;
        int ovector[186];
        
        pcre *r =  pcre_compile(re, PCRE_CASELESS|PCRE_DOTALL, &error, &erroffset, NULL);
        int rc = pcre_exec(r, NULL, txt, strlen(txt), 0, 0, ovector, 186);
        printf("%s\n",txt);
        
        if(rc<0)
            return txt;
        
        //printf("%i",ovector[0]);
        //char c1[1024];
        //pcre_copy_substring(txt, ovector, rc,1,c1, 1024);
        txt=stringReplace(txt,ovector[0],ovector[2],"");
        return txt;
    }
    
    
    char* stringReplace(char* subj,int start,int end,char* replace)
    {
        int newLength=strlen(subj)-(end-start+1)+strlen(replace);
        char newString[newLength];
        int i;
        int itr=0;
        for(i=0;i<strlen(subj);i++)
        {
            if(i<start || i>end)
            {
                newString[itr]=subj[i];
                itr++;
            }    
        } 
        newString[newLength]='\0';
        return newString;
    }
    Last edited by aboosoyeed; 03-22-2012 at 01:02 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're returning the local array (pointer) newString in stringReplace.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    15
    Quote Originally Posted by oogabooga View Post
    You're returning the local array (pointer) newString in stringReplace.
    But it works when i run it first time .. its only when i use the same function second time that the problem happens .. and the problem happens before the invocation of stringReplace. u can see my print statement in pcreReplace

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's still incorrect to return a pointer to data on a function's stack since it is invalid after the function returns (and will soon be overwritten with other data).

    Are you using ovector properly?
    The first pair of integers, ovector[0] and ovector[1], identify the portion of the subject string matched by the entire pattern. The next pair is used for the first capturing subpattern, and so on.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    15
    yeah . That seemed to be the problem .I converted from array to pointer and now its working fine. Though do u have ne idea how to find capture repeating patters. for example if there are two matching patterns like @xyz and @awe

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just use a loop. Here's an example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pcre.h>
    
    void print(char *str, int start, int end) {
        while (start <= end)
            putchar(str[start++]);
        putchar('\n');
    }
    
    int main(void) {
        char str[] = "@abcd whatever @ef @ghi stuff";
        int errOffset;
        int rc;
        const char *errText;
        int ovector[6];
        pcre *re;
        int offset = 0;
    
        re = pcre_compile("@\\S+", 0, &errText, &errOffset, NULL);
        if (re == NULL) {
            fprintf(stderr, "Error: %s (%d)\n", errText, errOffset);
            exit(1);
        } else {
            while (1) {
                rc = pcre_exec(re, NULL, str, strlen(str), offset, 0, ovector, 6);
                if (rc <= 0)
                    break;
                print(str, ovector[0], ovector[1]);
                offset = ovector[1] + 1;  // move offset past current match
            }
            pcre_free(re);
        }
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    15
    Thank you . Appreciate your prompt reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-01-2011, 07:05 AM
  2. String issue
    By HighSeraphim in forum C Programming
    Replies: 4
    Last Post: 11-04-2009, 08:22 AM
  3. String issue
    By cszym001 in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2008, 01:28 PM
  4. string issue
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2007, 08:29 PM
  5. string issue
    By gregulator in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2005, 07:49 PM