C 具有复杂正则表达式-regex.h的分段故障

C 具有复杂正则表达式-regex.h的分段故障,c,regex,segmentation-fault,C,Regex,Segmentation Fault,我试图在一大块文本中查找URL regex_t reg; const char *regex="REGEXGOESHERE"; regmatch_t matches[16]; //Read data into variable filecontent regcomp(&reg, regex, REG_EXTENDED); int offset=0; int j; int found=0; int start,end; while( regexec(&reg, filecon

我试图在一大块文本中查找URL

regex_t reg;

const char *regex="REGEXGOESHERE";
regmatch_t matches[16];

//Read data into variable filecontent
regcomp(&reg, regex, REG_EXTENDED);

int offset=0;
int j;
int found=0;
int start,end;
while( regexec(&reg, filecontent+offset, 16, matches, 0) == 0)
{
    printf("\n\n");
    start = matches[0].rm_so+offset;
    end = matches[0].rm_eo-1+offset;

    printf("regex /%s/ at bytes %d-%d\n",
                regex, start, end);

    for (j=start; j<=end; j++)
    {
        printf("%c",filecontent[j]);
    }
    offset += matches[0].rm_eo;
    found = 1;
}
close(f);
然后运行它就会出现分段错误


可能出了什么问题

您需要检查regcomp的返回,它会告诉您您的regex无效

#include <regex.h>
#include <stdio.h>

int main() {
  regex_t reg;

  const char *regex="(https?:\\/\\/.*\\.(?:png|jpg))";
  regmatch_t matches[16];
  int ret;
  char err[1024];

  //Read data into variable filecontent
  ret = regcomp(&reg, regex, REG_EXTENDED);
  if (ret != 0) {
        regerror(ret, &reg, err, 1024);
        printf("%s\n", err);
        return 1;
  }

  regfree(&reg);
  return 0;
}
#包括
#包括
int main(){
regex_t reg;
const char*regex=“(https?:\\/\\/.\\.\。(?:png|jpg))”;
regmatch_t matches[16];
int ret;
字符错误[1024];
//将数据读入变量filecontent
ret=regcomp(®,regex,regu扩展);
如果(ret!=0){
regerror(ret,®,err,1024);
printf(“%s\n”,err);
返回1;
}
regfree(®);
返回0;
}
您将得到
无效的前置正则表达式


原因是
(?:
POSIX正则表达式不支持它,即使是扩展的正则表达式也不支持它。

潜在的noob问题:为什么在
https?://
?segfault发生在哪一行?如果不知道,添加一些打印语句或使用调试器来查找。我首先检查regcomp的返回值;如果您的regex有一个错误,reg的内容将未定义。
#include <regex.h>
#include <stdio.h>

int main() {
  regex_t reg;

  const char *regex="(https?:\\/\\/.*\\.(?:png|jpg))";
  regmatch_t matches[16];
  int ret;
  char err[1024];

  //Read data into variable filecontent
  ret = regcomp(&reg, regex, REG_EXTENDED);
  if (ret != 0) {
        regerror(ret, &reg, err, 1024);
        printf("%s\n", err);
        return 1;
  }

  regfree(&reg);
  return 0;
}