Консультация № 175589
24.12.2009, 16:37
35.00 руб.
0 3 1
Здраствуйте уважаемые эксперты.
Пишу на с++ в codebloks возникла задача распарсить текст использую библиотеку pcre.
Вот фаил с текстом его необходими распарсить и вытащить эту строку :
http://fileshare245.depositfiles.com/auth-1261655739bede5d845b7b1f335a0176-95.158.205.93-969473376-34511674-guest/FS245-8/x_zone.rar

Вот как пишу:
Код:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <pcre.h>

using namespace std;
int main()
{
fstream file_buff;
file_buff.open("file",ios::in );
string buffer;
file_buff >>buffer;

char regx[] = "\w+.\/\/\w+\d+\.\w+\.\w+\/\w+-.+\.\w";

pcre *re;
int options = 0;
const char *error;
int erroffset;
re = pcre_compile (regx, options, &error, &erroffset, NULL);

if (!re)
{
cout << "Failed\n";
}
else
{
int count = 0;
int ovector[30];
cout << "Парсинг... " << endl;
count = pcre_exec (re, NULL, buffer.c_str(),buffer.length(), 0, 0, ovector, 30);
if (count< 0)
{
switch(count)
{
case PCRE_ERROR_NOMATCH:
cout << "Нет совпадений" << endl;
return -1;
default:
cout << "Ошибка : " <<count <<endl;
return -1;
}
free(re); /* Release memory used for the compiled pattern */
return 1;
}
else
{
for (int c = 0; c < 2 * count; c += 2)
{
if (ovector[c] < 0)
{
cout << "\n";
}
else
{
cout << ovector[c] << "/" << ovector[c + 1] << "\n";
}
}
}
}
return 0;
}


Постоянно выдает: нет совпадений
Может что-то не правильно пишу или выражение составлено не правильно?
Как вытащить строку???
OS ubuntu 9.10 x64, codebloks.

Обсуждение

Неизвестный
24.12.2009, 16:43
общий
попробуйте
char regx[] = "\\w+.\\/\\/\\w+\\d+\\.\\w+\\.\\w+\\/\\w+-.+\\.\\w";
"" в строках C специальный символ и его нужно экранировать.
Неизвестный
24.12.2009, 16:51
общий
Цитата: 303901
попробуйте
char regx[] = "\\w+.\\/\\/\\w+\\d+\\.\\w+\\.\\w+\\/\\w+-.+\\.\\w";
"" в строках C специальный символ и его нужно экранировать.

Не находит все равно!
Неизвестный
24.12.2009, 17:42
общий
это ответ
Здравствуйте, Akum2008.
поправил первые попавшиеся на глаза ошибки. Возможно есть еще, но вроде работает.
Код:
$ g++ -lpcre -o t t.cpp
$ ./t
Парсинг...
9782/9914
http://fileshare245.depositfiles.com/auth-1261655739bede5d845b7b1f335a0176-95.158.205.93-969473376-34511674-guest/FS245-8/x_zone.rar


Приложение:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <pcre.h>

using namespace std;
int main()
{
fstream file_buff;
file_buff.open("file",ios::in );
string buffer;
// >> will read only till first space. We have to read whole stream into a string.
// file_buff >>buffer;
buffer.resize(1024*1024*16+1,0);//Resize string to 16MB. Will the page grow above 16MB?...
file_buff.getline((char *)buffer.c_str(),1024*1024*16,0);//Read it
//correct regexp using look ahead and look behind constructs
//will fetch all deposit auth links
char regx[] = "(?<=")http://fileshare[0-9]+\\.depositfiles\\.com/auth[^"]+(?=")";

pcre *re;
int options = 0;
const char *error;
int erroffset;
re = pcre_compile (®x[0], options, &error, &erroffset, NULL);

if (!re)
{
cout << "Failed\n";
}
else
{
int count = 0;
int ovector[30];
cout << "Парсинг... " << endl;
count = pcre_exec (re, NULL, buffer.c_str(),buffer.length(), 0, 0, ovector, 30);
if (count< 0)
{
switch(count)
{
case PCRE_ERROR_NOMATCH:
cout << "Нет совпадений" << endl;
return -1;
default:
cout << "Ошибка : " <<count <<endl;
return -1;
}
// release memory correctly!
pcre_free(re); /* Release memory used for the compiled pattern */
return 1;
}
else
{
for (int c = 0; c < 2 * count; c += 2)
{
if (ovector[c] < 0)
{
cout << "\n";
}
else
{
cout << ovector[c] << "/" << ovector[c + 1] << "\n";
//copy match to temporary buffer
string match=buffer.substr(ovector[c],ovector[c+1]-ovector[c]);
//and output it
cout << match <<"\n";
}
}
}
}
return 0;
}
5
Спасибо разобрался!
Форма ответа