Thread: about regular expression

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    about regular expression

    below code for searching relative path for resource in html text,but when using regcomp method to complie the regular expression,a error is printed:Invalid preceding regular expression;I dont know if <regex.h> does not support (?! xxx ) or something else make the error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <regex.h>
    
    //for searching relative path
    #define SRC_RELATIVE "src\\s*=\\s*\"?(?!http|/|\"http|\"/)\\S+\\.%s\"?"
    
    //for test
    char *s= " src=aaa.gif src=\"aaa.gif\" \
    Src = /aaa/aaa.gif src = \"/aaa/aaa.gif\" \
    src = http://www.site.com/aaa.gif src = \"http://www.site.com/aaa.gif\" ";
    
    
    char *get_regerror (int errcode, regex_t *compiled){
          size_t length = regerror (errcode, compiled, NULL, 0);
          char *buffer = malloc(length);
          regerror (errcode, compiled, buffer, length);
          return buffer;
    }
    
    void doit(char *src,char *reg,char *restype){
          char line[100];
          char *c = src;
          char result[100];
          regex_t r;
          regmatch_t match;
          int i;
    
          bzero(line,sizeof(line));
          snprintf(line,sizeof(line),reg,restype);
    
          if((i = regcomp(&r,line,REG_EXTENDED | REG_ICASE)) != 0){
                fprintf(stderr,"%s\n",get_regerror(i,&r));
                exit(1);
          }
          while((i = regexec(&r,c,1,&match,0)) == 0) {
                bzero(result,sizeof(result));
                int eo = (int)match.rm_eo;
                int so = (int)match.rm_so;
                memcpy(result,c + so,eo - so);
                printf("%s\n",result);
                c += match.rm_eo;//for another match
          }
          regfree(&r);
    }
    
    int main(){
          printf("relative path:\n");
          doit(s,SRC_RELATIVE,"gif");
    
          return 0;
    }
    Last edited by asert; 11-07-2006 at 06:48 AM.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    REG_BADRPT is your return code - I assume.

    The leading ? character doesn't have a preceding regular expression -
    (?!http|/|\"http|\"/) the leading "?" is invalid - this version of the regex engine does not support it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Regular Expression
    By tintifaxe in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 07:16 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Regular Expression Troubles
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2002, 04:21 PM