2013-06-06 6 views
-2

새 C++ 11 정규식 라이브러리가있는 문서에서 field = value 쌍을 어떻게 추출합니까? 문서 (표준으로 취급 : 문자열)의C++ 11 regex (쌍 추출)

예 :

캡션 = "CALC.EXE"; CommandLine = "\"C : \ Windows \ system32 \ calc.exe \ ""; CreationClassName = "Win32_Process"; CreationDate = "20130606162432.173628 + 240"; CSCreationClassName = "Win32_ComputerSystem"Handle = "13484"; HandleCount = 93;

출력으로 내가 얻을 필요가지도 : 나는 어쩌면 그렇게 원하는 것을

{"Caption", "calc.exe"} 
{"CommandLine", "\"C:\\Windows\\system32\\calc.exe\" "} 
{"CreationClassName", "Win32_Process"} 
{"CreationDate", "20130606162432.173628+240"} 
{"CSCreationClassName", "Win32_ComputerSystem"} 
{"Handle", "13484"} 
{"HandleCount", "93"} 

코드 :

Todo

+0

은 할 수 없습니다 세미콜론으로 토큰 화 한 다음 공백으로 토큰 화 하시겠습니까? – VoronoiPotato

+0

@ VoronoiPotato는 몇 가지 예제 코드를 제공 할 수 있습니까? 호기심이없는 – Edward83

+0

, 어떤 컴파일러를 사용하고 있습니까? AFAIKnew, ''gcc가 깨졌습니다. – blue

답변

1

내가 일, 를 만들었지 만 정말에만 3.2 ++ 그 소리에 일의 libC++는 (즉, Mac의 경우) :

#include <iostream> 
#include <string> 
#include <regex> 

using namespace std; 

int main(int, char**) { 
    string input; 
    getline(cin, input, '\0'); 
    cout << input; 

    regex rx { string { R"---((\w+) *= *([^;]*))---" } }; 
    for(sregex_iterator E, i { input.begin(), input.end(), rx }; i != E; ++i) { 
    cout << "match: (" << (*i)[1].str() << ")(" << (*i)[2].str() << ")" << endl; 
    } 

    return 0; 
} 
+0

도와 주셔서 감사합니다. 너는 잘 했어! 내가 너에게 줄께;) – Edward83

+0

당신을 환영합니다! – Massa

1

는 다음 텍스트 필드와 일치하는 정규 표현식 만들기 '='부호 다음에 ';'가 오는 텍스트 필드가옵니다. 해당 일반 표현식을 대상 텍스트에 적용하는 regex_iterator 개체를 만듭니다. 완료 될 때까지 반복합니다.

+0

힌트 감사합니다;) – Edward83

1

gcc/libstdC++ regex work를 만들 수 없습니다. 내가 할 수있는 최선의 방법은 문제를 해결하는 것입니다.

#include <iostream> 
    #include <string> 

    using namespace std; 

    class unquot { 
    string where; 
    char what, quote; 
    public: 
    unquot(char _what = '"', char _quote = '\\') : what(_what), quote(_quote) {} 
    string str() { return where; } 

    friend istream& operator>>(istream& i, unquot& w) { 
     w.where = string(); 

     char c = i.get(); 
     if(!i) 
     return i; 

     if(c != w.what) { 
     i.setstate(ios::failbit); 
     i.putback(c); 
     return i; 
     } 

     bool quoted = false; 
     for(c = i.get(); i; c = i.get()) { 
     if(quoted) { 
      w.where.push_back(c); 
      quoted = false; 
     } else if(c == w.quote) { 
      quoted = true; 
     } else if(c == w.what) { 
      break; 
     } else { 
      w.where.push_back(c); 
     } 
     } 

     return i; 
    } 
    }; 

    class until { 
    string where; 
    char what, quote; 
    public: 
    until(char _what = '"', char _quote = '\\') : what(_what), quote(_quote) {} 
    string str() { return where; } 

    friend istream& operator>>(istream& i, until& w) { 
     w.where = string(); 

     char c = i.get(); 
     if(!i) 
     return i; 

     if(c != w.what) { 
     i.setstate(ios::failbit); 
     i.putback(c); 
     return i; 
     } 

     w.where.push_back(c); 

     bool quoted = false; 
     for(c = i.get(); i; c = i.get()) { 
     w.where.push_back(c); 
     if(quoted) { 
      quoted = false; 
     } else if(c == w.quote) { 
      quoted = true; 
     } else if(c == w.what) { 
      break; 
     } 
     } 

     return i; 
    } 
    }; 

    class word { 
    string where; 
    public: 
    word() {} 
    string str() { return where; } 

    friend istream& operator>>(istream& i, word& w) { 
     bool before = true, during = false; 
     w.where = string(); 

     for(char c = i.get(); i; c = i.get()) { 
     bool wordchar = isalnum(c) || (c == '_'); 
     bool spacechar = isspace(c); 
     bool otherchar = !wordchar && !spacechar; 

     if(before) { 
      if(wordchar) { 
      swap(before, during); 
      w.where.push_back(c); 
      } else if(otherchar) { 
      i.setstate(ios::failbit); 
      i.putback(c); 
      break; 
      } 
     } else if(during) { 
      if(wordchar) { 
      w.where.push_back(c); 
      } else if(otherchar) { 
      i.putback(c); 
      break; 
      } else { 
      during = false; 
      } 
     } else { 
      if(!spacechar) { 
      i.putback(c); 
      break; 
      } 
     } 
     } 
     return i; 
    } 
    }; 

    class skip { 
    char which; 
    public: 
    skip(char _which) : which(_which) {} 
    friend istream& operator>>(istream& i, skip& s) { 
     bool before = true; 
     for(char c = i.get(); i; c = i.get()) { 
     if(c == s.which) { 
      before = false; 
     } else if(!isspace(c)) { 
      i.putback(c); 
      if(before) 
      i.setstate(ios::failbit); 
      break; 
     } 
     } 
     return i; 
    } 
    }; 



    int main() 
    { 
    word w; 
    skip eq { '=' }; 
    unquot q; 
    skip semi { ';' }; 
    while(cin >> w >> eq >> q >> semi) { 
     cout << w.str() << " {" << q.str() << "}" << endl; 
    } 

    return 0; 
    } 

"until q;"를 사용할 수 있습니다. "unquot q;"대신 " 당신이 따옴표를 유지하려면 ...

+0

검색하는 것이 아니라 감사합니다;) – Edward83

+0

당신을 환영합니다. 나는 g ++과 clang ++, 그리고 libstdC++로 좀 더 테스트를했다. 정규식 함수 defs 여기에있는 libstdC++ 버전에 대한 누락 된 것으로 보인다. MacOSX에서 libC++와 작동한다고 가정합니다.하지만 테스트 할 시간이 없었습니다. 행운을 빕니다! – Massa