如何在C中使用PCRE进行多个匹配

如何在C中使用PCRE进行多个匹配,c,regex,shared-libraries,pcre,C,Regex,Shared Libraries,Pcre,我正在尝试使用C的PCRE库,然后使用regex从网页匹配到美国参议员的多个链接 为此,我需要正则表达式能够为我返回100个匹配项,以便我可以将网址打印到电子邮件中 根据我的研究,PCRE库似乎是实现这一点的方法,但我不知道如何从字符串中获取多个匹配项 这是我将要使用的正则表达式模式 联系人:\s+]*?\s+?href=([“'])(.*)\1 这是我将要使用的当前代码 #include <stdio.h> #include <stddef.h> #include &l

我正在尝试使用C的PCRE库,然后使用regex从网页匹配到美国参议员的多个链接

为此,我需要正则表达式能够为我返回100个匹配项,以便我可以将网址打印到电子邮件中

根据我的研究,PCRE库似乎是实现这一点的方法,但我不知道如何从字符串中获取多个匹配项

这是我将要使用的正则表达式模式

联系人:\s+]*?\s+?href=([“'])(.*)\1

这是我将要使用的当前代码

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <pcre.h>

int main() {


    // initiate all used Variables
    FILE *file;
    char *buffer;
    long size;

    //Wget on Senate webpage
    system("wget -q http://www.senate.gov/general/contact_information/senators_cfm.cfm");


    // Attempt to open file
    file = fopen("senators_cfm.cfm", "r");

    if(file == NULL){

        printf("Was unable to open file \n");
        return 1;        

    }

    //Attempt to read to end of file
    fseek(file, 0L, SEEK_END);



    //Determine the number of bytes that were in the file
    size = ftell(file);

    //Attempt to allocate the number of bytes needed
    buffer = (char*) calloc(size, sizeof(char));    
    if(buffer == NULL){

        printf("Unable to allocate memory needed \n");
        return 1;
    }


    //Reset the reader to start of file
    rewind(file);


    //Read whole file into buffer
    fread(buffer, sizeof(char), size, file);


    //Close file
    fclose(file);


    //Free all information that we allocated memory for
    free(buffer);

    unlink("senators_cfm.cfm");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){
//启动所有使用的变量
文件*文件;
字符*缓冲区;
长尺寸;
//参议院网页上的工作组
系统(“wget-qhttp://www.senate.gov/general/contact_information/senators_cfm.cfm");
//尝试打开文件
file=fopen(“senators_cfm.cfm”,“r”);
if(file==NULL){
printf(“无法打开文件\n”);
返回1;
}
//尝试读取到文件末尾
fseek(文件,0L,SEEK\u结束);
//确定文件中的字节数
大小=ftell(文件);
//尝试分配所需的字节数
buffer=(char*)calloc(size,sizeof(char));
if(buffer==NULL){
printf(“无法分配所需的内存\n”);
返回1;
}
//将读取器重置为文件的开头
倒带(文件);
//将整个文件读入缓冲区
fread(缓冲区、大小、文件);
//关闭文件
fclose(文件);
//释放我们为其分配内存的所有信息
自由(缓冲);
取消链接(“senators_cfm.cfm”);
返回0;
}

你是否考虑过使用支持字符串作为原始数据类型的编程语言,甚至可能在标准库中有HTTP客户端?它是本课程使用C或C++的一部分(我以前没有使用C++)然后我想同时使用正则表达式。我有点喜欢这种方法。如果你在
buffer
中有页面,并且只想刮取所有的电子邮件和网页,为什么不使用
strstr
for
“http“
strchr
'@'
,然后将每个链接或电子邮件括起来并提取出来?@DavidC.Rankin,对不起,我想我没有完全理解你的意思。你是在建议我做一些类似while循环的事情,继续尝试查找电子邮件的链接网址,直到我找不到为止?请注意,如果你的目标是POSIX系统,你可以直接从标准库中使用非PCRE正则表达式引擎