0

So I need to parse such string login=julius&password=zgadnij&otherArg=Value with N args and each arg will have a value. You can find such ti GET arguments and in POST requests. So how to create a parser for such strings using Boost?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Rella
  • 65,003
  • 109
  • 363
  • 636

2 Answers2

3
  • split on &
  • split the resulting parts on =
  • URL-decode both (!) the name and the value part

No regex needed.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

In this question's case, as Tomalak mentioned, regular expression may be a little overkill. If your real input is more complex and regular expression is needed, does the following code illustrate the usage?

int main() {
  using namespace std;
  using namespace boost;
  string s = "login=julius&password=zgadnij&otherArg=Value";
  regex re_amp("&"), re_eq("=");
  typedef sregex_token_iterator sti;
  typedef vector< string > vs;
  typedef vs::iterator vsi;
  sti i( s.begin(), s.end(), re_amp, -1 ), sti_end;
  vs config( i, sti_end ); // split on &

  for ( vsi i = config.begin(), e = config.end();  i != e;  ++ i ) {
    // split on =
    vs setting( sti( i->begin(), i->end(), re_eq, -1 ), sti_end );
    for ( vsi i2 = setting.begin(), e2 = setting.end();  i2 != e2;  ++ i2 ) {
      cout<< *i2 <<endl;
    }
  }
}

Hope this helps

Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26