2011-10-24 11 views
1

今、私は "href =" http://www.AAA.com ""やその他の文字のような多くの部分文字列を持っています ここで私の質問コードを書く:C言語でpcreに適切なパターンを書くには

文字パターン[] = "/^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パターン[] = "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,but私はすべてのURLを選ぶことができます、進んでいただきありがとうございます – Hession

関連する問題