2009-09-14 60 views
5

我对使用C没有经验,我需要使用PCRE来获得匹配。
这里是我的源代码示例:如何使用PCRE获取所有匹配组?

int test2() 
{ 
    const char *error; 
    int erroffset; 
    pcre *re; 
    int rc; 
    int i; 
    int ovector[OVECCOUNT]; 

    char *regex = "From:([^@]+)@([^\r]+)"; 
    char str[] = "From:[email protected]\r\n"\ 
        "From:[email protected]\r\n"\ 
        "From:[email protected]\r\n"; 

    re = pcre_compile (
      regex,  /* the pattern */ 
      0,     /* default options */ 
      &error,    /* for error message */ 
      &erroffset,   /* for error offset */ 
      0);     /* use default character tables */ 

    if (!re) { 
     printf("pcre_compile failed (offset: %d), %s\n", erroffset, error); 
     return -1; 
    } 

    rc = pcre_exec (
     re,     /* the compiled pattern */ 
     0,     /* no extra data - pattern was not studied */ 
     str,     /* the string to match */ 
     strlen(str),   /* the length of the string */ 
     0,     /* start at offset 0 in the subject */ 
     0,     /* default options */ 
     ovector,    /* output vector for substring information */ 
     OVECCOUNT);   /* number of elements in the output vector */ 

    if (rc < 0) { 
     switch (rc) { 
      case PCRE_ERROR_NOMATCH: 
       printf("String didn't match"); 
       break; 

      default: 
       printf("Error while matching: %d\n", rc); 
       break; 
     } 
     free(re); 
     return -1; 
    } 

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

在本演示中,输出仅是:

0: From:[email protected]
1: regular.expressions
2: example.com

我要输出所有的火柴;我怎样才能做到这一点?

+0

不要使用正则表达式,而要使用真正的解析器。邮件协议允许的不仅仅是简单的邮箱地址。 – Gumbo 2009-09-14 14:11:46

+0

这是pcre的演示,我只想知道如何在匹配组中使用pcre。 感谢您的评论。 – tbmvp 2009-09-15 01:57:14

+0

你应该参考这篇文章:http://stackoverflow.com/questions/7785557/pcre-match-all-groups-in-c – soulmachine 2011-11-29 08:51:24

回答

6

我使用类来包装PCRE,以使这更容易,但在pcre_exec后,ovector包含子字符串索引,你需要找到原始字符串内的匹配。

因此,这将是这样的:

#include <string> 
#include <iostream> 
#include "pcre.h" 

int main (int argc, char *argv[]) 
{ 
    const char *error; 
    int erroffset; 
    pcre *re; 
    int rc; 
    int i; 
    int ovector[100]; 

    char *regex = "From:([^@]+)@([^\r]+)"; 
    char str[] = "From:[email protected]\r\n"\ 
        "From:[email protected]\r\n"\ 
        "From:[email protected]\r\n"; 

    re = pcre_compile (regex,   /* the pattern */ 
         PCRE_MULTILINE, 
         &error,   /* for error message */ 
         &erroffset,  /* for error offset */ 
         0);    /* use default character tables */ 
    if (!re) 
    { 
     printf("pcre_compile failed (offset: %d), %s\n", erroffset, error); 
     return -1; 
    } 

    unsigned int offset = 0; 
    unsigned int len = strlen(str); 
    while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0) 
    { 
     for(int i = 0; i < rc; ++i) 
     { 
      printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]); 
     } 
     offset = ovector[1]; 
    } 
    return 1; 
} 
+0

感谢您的回答。 但我仍然不知道如何输出所有的比赛。 – tbmvp 2009-09-15 02:00:20

+0

你是否只获得第一套matcheS?编译正则表达式时,必须指定PCRE_MULTILINE选项。详情请参阅:http://www.pcre.org/pcre.txt。我会更新这个例子。 – 2009-09-15 12:04:58

+0

我已经更新了我的答案中的代码,以执行我认为您需要的操作。我承认不是PCRE专家,因为我只是通过包装使用它,所以我不熟悉它的错综复杂。我想可以通过1次调用exec来做到这一点。并让它返回具有字符串索引的ovector数组到所有匹配项。这应该可以做到这一点。 – 2009-09-15 13:02:22

5

注:pcre_exec的最后一个参数()必须是元素计数,不是的sizeof()! (http://www.pcre.org/readme.txt

+1

另外:元素数必须是3的倍数(例如90不是100!) – glob 2011-06-19 11:28:40

+0

http://regexkit.sourceforge.net/Documentation/pcre/pcre_exec.html – glob 2011-06-19 11:28:48