Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > PCRE: получение всех совпадений


Автор: Alca 28.2.2011, 17:25
Пишу на СИ (C99), Kubuntu. PCRE 8.02 (2010-03-19)
Число совпадений должно быть 3, а не 1. (хотя с имейлами все гуд)
Код

#include <stdio.h>
#include <stdlib.h>
#include <pcre.h>
//----------------------------------------------------------------------------------------------------
struct SResponseMemory {
    char   *pszMemory;      
    size_t  uiSize;
};
typedef struct SResponseMemory SResponseMemory;

int
bTest(const SResponseMemory *pcrmResponseContent, const char *pcszRegExpr);
//----------------------------------------------------------------------------------------------------
int
main() {
    SResponseMemory crmResponseContent;

    //!!!здесь лажа!!!
    //crmResponseContent.pszMemory = "__test__test__test__";
    //crmResponseContent.uiSize    = strlen(crmResponseContent.pszMemory);
    //
    //const char cszRegExpr[]      = "test";

    //здесь ок
    //crmResponseContent.pszMemory = "From:regular.expressions@example.com_From:exddd@43434.com_From:7853456@exgem.com";
    //crmResponseContent.uiSize    = strlen(crmResponseContent.pszMemory);
    //
    //const char  cszRegExpr[]     = "From:([^@]+)@([^\r]+)";

    //здесь ок
    //crmResponseContent.pszMemory = "regular expressions=very cool";
    //crmResponseContent.uiSize    = strlen(crmResponseContent.pszMemory);

    //const char  cszRegExpr[]     = "([^=]*)=(.*)";

    bTest(&crmResponseContent, cszRegExpr);

    return EXIT_SUCCESS;
}
//----------------------------------------------------------------------------------------------------
int
bTest(const SResponseMemory *pcrmResponseContent, const char *pcszRegExpr) {
    pcre       *preRegExpr = NULL;
    int         iOptions   = PCRE_CASELESS | PCRE_DOTALL | PCRE_MULTILINE;
    const char *cpszError  = NULL;
    int         iErrOffset = 0;

    //--------------------------------------------------
    //compile
    preRegExpr = pcre_compile (    /* http://www.manpagez.com/man/3/pcre_compile/ */
            pcszRegExpr,
            iOptions,
            &cpszError,
            &iErrOffset,
            NULL);

    if (NULL == preRegExpr) { // в случае ошибки компиляции
       printf("Failed\n");

       return EXIT_FAILURE;
    }

    //--------------------------------------------------
    //execute
    int iCount = 0;
    int aiOutVector[30];

    iCount = pcre_exec(  /*http://www.manpagez.com/man/3/pcre_exec/*/
               preRegExpr,
               NULL,
               pcrmResponseContent->pszMemory,
               pcrmResponseContent->uiSize,
               0,
               0,
               aiOutVector, 30);
    printf("iCount: %i\n", iCount);    // iCount всегда равен 1, почему? хотя должен быть равным 3

    //...

    return EXIT_SUCCESS;
}
//---------------------------------------------------------------------------


http://www.pcre.ru/eval/es%40%3D%23%3D%40101%40%3D%23%3D%40_test___test___test_/

Автор: Alca 1.3.2011, 19:34
Кажеться это из-зи флага PCRE_GLOBAL (http://web.cecs.pdx.edu/~postj/contest/pcregsub.html).
Но в pcre.h такого флага нет.  smile 

Автор: Alca 2.3.2011, 11:40
Короче надо циклить pcre_exec

http://stackoverflow.com/questions/1421785/how-can-i-use-pcre-to-get-all-match-groups

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)