2011-10-24 2 views
1

이제 "href ="http://www.AAA.com ""및 다른 문자와 같은 많은 하위 문자열이 있습니다 여기 내 질문에, 내 C에서 코드 작성 :C와 함께 pcre에 적절한 패턴을 작성하는 방법

char pattern [] = "/^href.*>$/g";

및 긴 문자열의 모든 URL을 선택하고 싶습니다. 하지만 그건 효과가 없어. 어떤 사람들이 나를 도와 줘? 귀하의 도움을 주시면 감사하겠습니다. 다음은 코드입니다 :

#define PCRE_STATIC // 
#include <stdio.h> 
#include <string.h> 
#include <pcre.h> 
#define OVECCOUNT 30 /* should be a multiple of 3 */ 
#define EBUFLEN 128 
#define BUFLEN 1024 

int main() 
{ 
    pcre *re; 
    const char *error; 
    int erroffset; 
    int ovector[OVECCOUNT]; 
    int rc, i; 
    char src[] = "<a href=\"http://union.elong.com/r/hotel/2000000000855850825\" target=\"_blank\">ss</a></td></tr><tr><td><a href=\"http://123.sogou.com/sub/fanyi.html\" targedd</a></td><td><a href=\"http://123.sogou.com/sub/fantizi.html\" target=\"_blank\">繁 体 字</a></td><td><a href=\"http://123.sogou.com/sub/kuaidi.htm>快递查询</a></td></tr><tr><td><a href=\"http://q.stock.sohu.com/index.shtm>股票行情</a></td><td><a href=\"http://www.chinamobile.com/service/billservice/>话费查询</a></td><td><a href=\"http://auto.sohu.com/s2004/weizhangchaxun.shtml>交通违章</a></td></tr><tr><td>"; 
    char pattern[] = "/^href.*>$/g"; 

    re = pcre_compile(pattern, 
         0, 
         &error, 
         &erroffset, 
         NULL); 

    if (re == NULL) { 
     printf("PCRE compilation failed at offset %d: %s\n", erroffset, error); 
     return 1; 
    } 
    rc = pcre_exec(re, 
        NULL, 
        src, 
        strlen(src), 
        0, 
        PCRE_MULTILINE, 
        ovector, 
        OVECCOUNT); 

    if (rc < 0) { 
     if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n"); 
     else printf("Matching error %d\n", rc); 
     pcre_free(re); 
     return 1; 
    } 
    printf("\nOK, %d has matched ...\n\n",rc); 
    for (i = 0; i < rc; i++) { 
     char *substring_start = src + ovector[2*i]; 
     int substring_length = ovector[2*i+1] - ovector[2*i]; 
     printf("$%2d: %.*s\n", i, substring_length, substring_start); 
    } 
    pcre_free(re); 
    return 0; 
} 
+0

개 코드를 입력하고, 원하는 출력을 입력 해주십시오. – FailedDev

+0

나는 char pattern [] = "href. *>"을 시도했다. 나 한테만 결과를 줄 수있어. 나는 모든 URL을 찾아야 해. – Hession

답변

1

이 정규 표현식을 사용해보십시오.

myregexp = pcre_compile("href\\s*=\\s*(['\"])(.*?)\\1", 0, &error, &erroroffset, NULL); 

샘플 코드 :

pcre *myregexp; 
const char *error; 
int erroroffset; 
int offsetcount; 
int offsets[(2+1)*3]; // (max_capturing_groups+1)*3 
myregexp = pcre_compile("href\\s*=\\s*(['\"])(.*?)\\1", 0, &error, &erroroffset, NULL); 
if (myregexp != NULL) { 
    offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (2+1)*3); 
    while (offsetcount > 0) { 
     // match offset = offsets[0]; 
     // match length = offsets[1] - offsets[0]; 
     if (pcre_get_substring(subject, &offsets, offsetcount, 0, &result) >= 0) { 
      // Do something with match we just stored into result 
     } 
     offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, offsets[1], offsets, (2+1)*3); 
    } 
} else { 
    // Syntax error in the regular expression at erroroffset 
} 
+0

감사합니다. 패턴 ("href \\ s * = \\ s * ([* \]) (. *?) \\ 1")을 사용해 보았지만 함수 pcre_exec()는 오류 번호 -3. – Hession

+0

@hession 이전에 패턴이 작동 했습니까? 패턴이 맞습니다. – FailedDev

+0

죄송합니다. 다시 시도했을 때 결과가 $ 0 : href = "http://union.elong.com/ r/hotel/2000000000855850825 " $ 1 :" $ 2 : http://union.elong.com/r/hotel/2000000000855850825하지만 모든 URL을 고를 수 있기를 바랍니다. 진보 감사합니다. – Hession

관련 문제