0
HTML Webページにすべてのtxtファイルを見つける簡単なプログラムを作成しようとしています。
libcurl(インターネットからページをダウンロードするため)とCを使い、ページをスキャンするためにPCREを使用しています。 (ところでこのコードはちょうどカールコールバックからである)のコードを実行している間、私はゼロの結果を得るC(PCRE)の正規表現
if(htmlContent == NULL) return;
char pattern[] = "/\\w+.txt/g";
const char *error;
int erroffset, ovector[OVECCOUNT], htmlLength = (int)(sizeof(htmlContent)/sizeof(char));
pcre *re = pcre_compile(pattern,0,&error,&erroffset,NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return;
}
int rc = pcre_exec(re,NULL,htmlContent,htmlLength,0,0,ovector,OVECCOUNT);
if(rc < 0) {
pcre_free(re);
return;
}
if (rc == 0)
{
rc = OVECCOUNT/3;
printf("ovector only has room for %d captured substrings\n", rc - 1);
}
int i;
for (i = 0; i < rc; i++)
{
char *substring_start = htmlContent + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}
- /\w+.txt/gし、次のコード -
私は、次のパターンを使用しています
[REを使用してHTMLを解析する](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)にnoと答えるだけです。 –
私はPCRE APIについてはわかりませんが、私はあなたの式で二重引用符を使うべきではないと思っています(愚かなPHPだけです)。また、ドットをエスケープしたいと思っています: '" \\ w + \\。 txt "' – Qtax
Qtaxは正しい:PCREライブラリを直接使うと、正規表現の区切り文字(この場合は '/')を*使用しない*。 PHPはそれらを必要とします。正規表現の構文がPerlのように見えるようにするために、文字列をPCREに渡す前にそれらを取り除きます。 'g'修飾子も必要ありません。 –