-2

如何field = value使用新的 C++ 11 正则表达式库从文档中提取对?

文档示例(将其视为 std::string):

Caption = "calc.exe"; 命令行 = "\"C:\Windows\system32\calc.exe\" "; CreationClassName = "Win32_Process"; 创建日期 = "20130606162432.173628+240"; CSCreationClassName = "Win32_ComputerSystem" 句柄 = "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"}

我想要的代码可能是这样的:

去做

4

3 回答 3

1

创建一个正则表达式,它匹配一个文本字段,后跟一个“=”符号,后跟一个文本字段,后跟一个“;”。创建一个regex_iterator将该正则表达式应用于目标文本的对象。迭代直到完成。

于 2013-06-06T17:50:34.677 回答
1

我无法让 gcc/libstdc++ 正则表达式工作。我能做的最好的似乎是解决你的问题是这样的:

  #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;
  }

您可以使用“直到 q;” 而不是“取消引用 q;” 如果你想保留引号......

于 2013-06-15T03:25:54.440 回答
1

我做了这项工作,但它实际上只适用于带有 libc++ 的 clang++ 3.2(即,在 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;
}
于 2013-06-18T10:58:57.047 回答