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;
}