当前位置: 技术问答>linux和unix
关于正则表达式
来源: 互联网 发布时间:2015-12-22
本文导语: 搜索relative path的正则表达式用regcomp编译报错:Invalid preceding regular expression;估计是(?!http|/|"http|"/)这部分正则表达式导致的;但是我把这些正则表达式移植到java下就没有问题;问一下是不是不支持还是其他原因?请问如...
搜索relative path的正则表达式用regcomp编译报错:Invalid preceding regular expression;估计是(?!http|/|"http|"/)这部分正则表达式导致的;但是我把这些正则表达式移植到java下就没有问题;问一下是不是不支持还是其他原因?请问如何解决这个问题
#include
#include
#include
#include
//for searching relative path
#define SRC_RELATIVE "src\s*=\s*"?(?!http|/|"http|"/)\S+\.%s"?"
//for searching absolute path
#define SRC_ABSOLUTE "src\s*=\s*"?/\S+\.%s"?"
//for searching url
#define SRC_URL "src\s*=\s*"?http\S+\.%s"?"
//for test
char *s= " src=/tech-qa-linux/aaa.gif"/tech-qa-linux/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)) != 0){
fprintf(stderr,"%sn",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("%sn",result);
c += match.rm_eo;//for another match
}
regfree(&r);
}
int main(){
printf("relative path:n");
doit(s,SRC_RELATIVE,"gif");
printf("nabsolute path:n");
doit(s,SRC_ABSOLUTE,"gif");
printf("nurl:n");
doit(s,SRC_URL,"gif");
return 0;
}
#include
#include
#include
#include
//for searching relative path
#define SRC_RELATIVE "src\s*=\s*"?(?!http|/|"http|"/)\S+\.%s"?"
//for searching absolute path
#define SRC_ABSOLUTE "src\s*=\s*"?/\S+\.%s"?"
//for searching url
#define SRC_URL "src\s*=\s*"?http\S+\.%s"?"
//for test
char *s= " src=/tech-qa-linux/aaa.gif"/tech-qa-linux/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)) != 0){
fprintf(stderr,"%sn",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("%sn",result);
c += match.rm_eo;//for another match
}
regfree(&r);
}
int main(){
printf("relative path:n");
doit(s,SRC_RELATIVE,"gif");
printf("nabsolute path:n");
doit(s,SRC_ABSOLUTE,"gif");
printf("nurl:n");
doit(s,SRC_URL,"gif");
return 0;
}
|
推荐搂主试一下 C++ 下最容易使用的正则表达式:
http://www.regexlab.com/deelx/
在国外网站上的讨论:
http://www.codeproject.com/article.asp?tag=19352982551739006
http://www.regexlab.com/deelx/
在国外网站上的讨论:
http://www.codeproject.com/article.asp?tag=19352982551739006