Good time of day. Need to parse the file, the file structure:

D-Date="02.05.2014 16:00:00"&D-NAS-Ident=10.11.81.66:50130&Acct-Status-Type=3&User-Name="ao250_5038202"&Event-Timestamp=1399032000&Acct-Delay-Time=0&NAS-Identifier="RST-E320-1"&Acct-Session-Id="erx TenGigabitEthernet 0/0/0.35042030:3504-2030:0887085679"&NAS-IP-Address=10.11.81.66&Service-Type=2&Framed-Protocol=1&Framed-Compression=0&ERX-Pppoe-Description="pppoe 00:22:b0:eb:d5:57"&Framed-IP.... 

Pull out D-Date , User-Name , NAS-IP-Address (For example, output D-Date="02.05.2014 16:00:00" )

That's what I managed to write, but refuses to search after the 1st similarity and the structure is not added. I ask for help.

 void __fastcall TForm4::Button1Click(TObject *Sender) { if(OpenDialog1->Execute()) { String Text = TFile::ReadAllText(OpenDialog1->FileName); Memo1->Lines->Add(Text); Application->ProcessMessages(); boost::wregex re( L"D-Date=\"(.*?)\"\&" L"User-Name=\"(.*?)\"\&" L"NAS-IP-Address=(.*?)\&"); boost::wcmatch m; if(boost::regex_search(Text.c_str(),m,re)) { for(size_t i=1; i< m.size(); ++i) Memo1->Lines->Add( m[i].str().c_str() ); Application->ProcessMessages(); } } 
  • You should describe the structure formally, and not by one example. (If you were given such a task, ask the one who gave the task.) In particular, you will have to answer the questions: what are the escape sequences? Are spaces to the left of the = sign possible? Are pieces without a sign = possible? Is a shielded = sign possible? Take action. - VladD
  • Sorry, I didn’t quite understand you. The file itself that needs to be sparced is yadi.sk/d/vIcYRISURhaGi , here is that if virustotal.com/ru/url/... for how many spaces there were, there are no gaps without = no, no. -------------------------------------------------- ----------------- unfortunately yes - woolf24
  • @ woolf24: “how much I looked, no” does not replace the formal specification. --- And how exactly is important? Part of the code before boost::wregex re( has no relation to the problem. Instead of Memo2->Lines->Add you could simply add to the list. And Application->ProcessMessages() shows that you do not know how to multithreaded programming (sorry for straightforward). - VladD
  • It is important to show the result of parsing in Memo no more than Memo2 was superfluous for me, just for the text parsing test, and the first one showed the read itself. - woolf24
  • @ woolf24, can I put a readable sample file somewhere? If all the records are of the same format as in the question, then you do not need any boost with regexps. It is enough to read a file line by line with 3 strstr and strchr for each line. - avp

1 answer 1

 #include <iostream> #include <string.h> #define MAX_NUMS 3 bool parse_kv(wchar_t* s, wchar_t* arr[MAX_NUMS]){ int n = 0; wchar_t* p = s; const wchar_t* tpls[MAX_NUMS] = { { L"D-Date=\""}, { L"User-Name=\"" }, { L"NAS-IP-Address="} }; const wchar_t* fmts[MAX_NUMS] = { { L"%*[^\"]%n" }, { L"%*[^\"]%n" }, { L"%*[^&]%n" } }; for(int i = 0; i < MAX_NUMS; ++i){ p = wcsstr(p, tpls[i]); if(p == NULL) return false; p += wcslen(tpls[i]); swscanf(p, fmts[i], &n); *(p + n) = L'\0'; arr[i] = p; p += (n + 1); } return true; } int main(void){ wchar_t s[] = L"..."; wchar_t* arr[MAX_NUMS]; if(parse_kv(s, arr)){ for(int i = 0; i < MAX_NUMS; ++i) wprintf(L"%ls\n", arr[i]); } else _putws(L"Error parse !!!"); getwchar(); return 0; } 
  • Uh ... And why on a clean, already transparent C? --- What are you going to do with the line Fake-D-Date = "01/01/2001 01:00:00" & D-Date = "05/02/2014 16:00:00" & ...? - VladD
  • @ woolf24: read text from a file is a simple task, boost is not needed for it. And you should be able to find it in Google in one minute. Do not be lazy. - VladD
  • @ woolf24: how were you looking? google "read + file + line + by + line" -> < stackoverflow.com/a/13035743/276994 > -> std :: ifstream file ("Read.txt"); std :: string str; while (std :: getline (file, str)) {// Process str} - VladD
  • @ woolf24: well, you write above: "there seems to be no file open file here." My answer is that it is not difficult to write it yourself. And the rest was shown to you. --- PS: There is a limit on the number of comments, delete the old ones. - VladD