簡體   English   中英

C語言中的正則表達式(PCRE)

[英]Regular Expression in C (PCRE)


我試圖編寫一個簡單的程序來查找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的模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM