2009-09-14 18 views
5

मैं सी का उपयोग करने में अनुभवहीन हूं, और मुझे मैच प्राप्त करने के लिए पीसीआरई का उपयोग करने की आवश्यकता है।
यहाँ मेरी स्रोत कोड का एक नमूना है:मैं सभी मिलान समूहों को प्राप्त करने के लिए पीसीआरई का उपयोग कैसे कर सकता हूं?

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

+0

यह पिक्चर के लिए एक डेमो है, मैं सिर्फ यह जानना चाहता हूं कि मिलान समूहों में पिक का उपयोग कैसे करें। आपकी टिप्पणी के लिए धन्यवाद। – tbmvp

+0

आपको इस पोस्ट का संदर्भ लेना चाहिए: http://stackoverflow.com/questions/7785557/pcre-match-all-groups-in-c – soulmachine

उत्तर

6

मैं इसे आसान बनाने के लिए पीसीआरई को लपेटने के लिए एक वर्ग का उपयोग करता हूं, लेकिन pcre_exec के बाद, ओवेक्टर में स्ट्रिंग इंडेक्स होते हैं जिन्हें आपको मूल स्ट्रिंग के भीतर मैचों को खोजने की आवश्यकता होती है।

तो यह होगा कुछ की तरह:

#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

+0

क्या आप केवल matcheS का पहला सेट प्राप्त कर रहे हैं? जब आप रेगेक्स संकलित करते हैं तो आपको पीसीRE_MULTILINE विकल्प निर्दिष्ट करना होगा। विवरण के लिए देखें: http://www.pcre.org/pcre.txt। मैं उदाहरण अपडेट करूंगा। –

+0

मैंने अपने उत्तर में कोड को अपडेट किया है जो मुझे लगता है कि आपको क्या चाहिए। मैं स्वीकार्य रूप से एक पीसीआरई विशेषज्ञ नहीं हूं क्योंकि मैंने इसे केवल एक रैपर के माध्यम से उपयोग किया है, इसलिए मैं इसकी जटिलताओं से परिचित नहीं हूं। मुझे लगता है कि इसे निष्पादित करने के लिए 1 कॉल के साथ ऐसा करने का एक तरीका होगा। और यह सभी मैचों में स्ट्रिंग इंडेक्स के साथ ओवेक्टर सरणी वापस कर दिया है। हालांकि यह चाल करना चाहिए। –

5

ध्यान दें: pcre_exec के अंतिम पैरामीटर() होना चाहिए तत्व गिनती, sizeof नहीं()! (http://www.pcre.org/readme.txt)

+1

इसके अलावा: तत्व गणना 3 का एक बहु होना चाहिए (उदाहरण के लिए 90 नहीं 100!) – glob

+0

http://regexkit.sourceforge.net/Documentation/pcre/pcre_exec.html – glob

संबंधित मुद्दे

 संबंधित मुद्दे