gpt4 book ai didi

c - 正则表达式似乎有误 - 找不到电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:09 24 4
gpt4 key购买 nike

我的函数在尝试查找电子邮件地址时遇到问题。我不知道可能是什么问题:(

static int contains_mail(const unsigned char *buffer, int length, int detmode)
{
const char *reg_exp = "([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z0-9._%+-]+)";

regex_t regex;
int reti;
regmatch_t matches[2];

int start0, end0, start1, end1;

reti = regcomp(&regex, reg_exp, REG_EXTENDED);

if(reti){ fprintf(stderr, "Could not compile regex\n"); exit(1); }

reti = regexec(&regex, buffer, 2, matches, 0);

start0 = matches[0].rm_so;
end0 = matches[0].rm_eo;
start1 = matches[1].rm_so;
end1 = matches[1].rm_eo;

printf("start0: %d", start0);
printf("end0: %d", end0);
printf("start1: %d", start1);
printf("end1: %d", end1);

if( !reti ){
//printf("1");
return 1;
} else {
//printf("0");
return 0;
}
}

示例输入文件:

dfo gpdf eriowepower riwope d@b.pl rwepoir weporsdfi dsfdfasdas@sdfaasdas.pl OSIDQOPWIEPOQWIE sdfs@asdsa.pl
WERO IWUEOIRU OWIERU WOIER asdas@asdasd.pl
aposidasop aposdi aspod iaspodi aspoid aspodi sdfsddfsd@asdasd.pl
werowerowe

看起来它开始于:

start0: 28end0: 28start1: 1end1: 8

但是看起来它不知道电子邮件的结尾是什么,所以我无法计算它:(

最佳答案

一个简单的问题,你是如何传递输入文件的?就好像我定义然后像下面这样调用它:

char string[] = "dfo gpdf eriowepower riwope d@b.pl rwepoir weporsdfi dsfdfasdas@sdfaasdas.pl OSIDQOPWIEPOQWIE sdfs@asdsa.pl\n\
WERO IWUEOIRU OWIERU WOIER asdas@asdasd.pl\n\
aposidasop aposdi aspod iaspodi aspoid aspodi sdfsddfsd@asdasd.pl\n\
werowerowe\n";

contains_mail(string, 0, 0);

并修改您的 contains_mail 函数以重复调用 regexec,如下所示:

reti = regexec(&regex, buffer, 2, matches, 0);
while (reti == 0) {
start0 = matches[0].rm_so;
end0 = matches[0].rm_eo;
start1 = matches[1].rm_so;
end1 = matches[1].rm_eo;

printf("start0: %d ", start0);
printf("end0: %d\n", end0);
printf("start1: %d ", start1);
printf("end1: %d\n", end1);
printf("email: %.*s\n", end1 - start1, buffer + start1);
buffer += end1;
reti = regexec(&regex, buffer, 2, matches, REG_NOTBOL);
}

我得到了所有的比赛:

$ ./email_regex
start0: 28 end0: 34
start1: 28 end1: 34
email: d@b.pl
start0: 19 end0: 42
start1: 19 end1: 42
email: dsfdfasdas@sdfaasdas.pl
start0: 18 end0: 31
start1: 18 end1: 31
email: sdfs@asdsa.pl
start0: 28 end0: 43
start1: 28 end1: 43
email: asdas@asdasd.pl
start0: 47 end0: 66
start1: 47 end1: 66
email: sdfsddfsd@asdasd.pl

我同意其他人的评论,您的正则表达式可能不是获取电子邮件地址的最佳选择。但您实际上想做什么?

关于c - 正则表达式似乎有误 - 找不到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23519973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com