gpt4 book ai didi

c - C 中的正则表达式 (PCRE)

转载 作者:行者123 更新时间:2023-11-30 18:05:09 25 4
gpt4 key购买 nike


我正在尝试编写一个简单的程序来查找 HTML 网页中的所有 txt 文件。
我使用 C 语言与 libcurl(以便从互联网下载页面)和 PCRE 来扫描页面。

我正在使用下一个模式 -/\w+.txt/g 和下一个代码 -

if(htmlContent == NULL) return;
char pattern[] = "/\\w+.txt/g";
const char *error;
int erroffset, ovector[OVECCOUNT], htmlLength = (int)(sizeof(htmlContent) / sizeof(char));
pcre *re = pcre_compile(pattern,0,&error,&erroffset,NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return;
}

int rc = pcre_exec(re,NULL,htmlContent,htmlLength,0,0,ovector,OVECCOUNT);
if(rc < 0) {
pcre_free(re);
return;
}
if (rc == 0)
{
rc = OVECCOUNT/3;
printf("ovector only has room for %d captured substrings\n", rc - 1);
}

int i;
for (i = 0; i < rc; i++)
{
char *substring_start = htmlContent + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}

运行代码时我得到零结果(顺便说一句,此代码仅来自curl回调)

最佳答案

模式应为“\\w+\\.txt\\b”\b 将阻止该模式匹配 foo.txtbar

关于c - C 中的正则表达式 (PCRE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6809445/

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