0

How best to parse the following string in c++: the input is a string, for example:

(someString1 45)(someString2 2432)(anotherString 55)  // .... etc.

of course we are interested with the string-name and value.. our goal is to save the string and value in a map. is there a automatic way to get the string inside the brackets ?

thank you,

Christophe
  • 68,716
  • 7
  • 72
  • 138
user3250354
  • 101
  • 10
  • Use `std::regex_match` [regex_match](http://stackoverflow.com/questions/19327562/regular-expression-for-parsing-data) – michal.z Dec 07 '14 at 13:39
  • 1
    There are *many* ways of "parsing" a string. It all depends on what you want and what problem you are trying to solve. – Some programmer dude Dec 07 '14 at 13:43
  • Does the string always not contain any whitespace? – Kerrek SB Dec 07 '14 at 13:43
  • If you only need to parse *this* string: `{{"someString1", 45},{"someString2", 2432},{"anotherString", 55}}`. If you need to parse many other strings, you need to *formally specify the language* you want to parse (as opposed to throwing together a couple of examples and calling it a day). Fringe cases *will* surface and bite you. – n. m. could be an AI Dec 07 '14 at 17:29

2 Answers2

1

A simple solution if your strings don't contain whitespace:

#include <iostream>
#include <string>

int main()
{
    char c1, c2;
    int n;
    std::string s;
    while (std::cin >> c1 >> s >> n >> c2 && c1 == '(' && c2 == ')')
    {
        std::cout << "Parse item: s = '" << s << "', number = " << n << "\n";
    }
}

This method only works on correct input and has no way of recovering mid-way. If you need that, you can build something somewhat more elaborate using getline with ) as the separator.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

The following will do the trick:

string some;  int n; 
string s = "(someString1 45)(someString2 2432)(anotherString 55)";
stringstream sst(s);   // to parse the string 

while (sst.get() == '(' && sst >> some >> n && sst.get()==')') {
    cout << some << "," << n << endl; 
}

This loop will not try to read some string and n if the open brace is not present.

A slight change could even allow to safely parse further input string if you'd expect something to follow the list of entries between braces:

string s = "(someString1 45)(someString2 2432)(anotherString 55)thats the rest";
...
while (sst.get() == '(') {  // open brace to process
    if (sst >> some >> n && sst.get() == ')') 
        cout << some << "," << n << endl;   // succesful parse of elements
    else {
        cout << "Wrong format !!\n";   // something failed 
        if (!sst.fail()) sst.setf(ios::failbit);  // case of missing closing brace
    }
} 
if (sst) { // if nothing failed, we are here because open brace was missing and there is still input 
    sst.unget();  // ready to parse the rest, including the char that was checked to be a brace
    string rest;
    getline(sst, rest);
    cout << "The braces are followed by:  " << rest << endl;
}
Christophe
  • 68,716
  • 7
  • 72
  • 138