2009-09-14 33 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

相關問題