I am trying to parse a text file the contains numeric data. I have a lot of lines that look like
129.3 72.7 121.6 173.6 203.3 120.7 40.5 79.2 94.0 123.2 165.8 178.8 135.5 78.5 66.2
but the length of the lines vary. Each line is also preceded by a few spaces.
I would like to use regular expressions to parse the line and place each number into an array that I can then manipulate later.
Using
std::getline(is, line);
std::tr1::regex rx("[0-9-\.]+");
std::tr1::cmatch res;
std::tr1::regex_search(line.c_str(), res, rx);
only matches the first number. If instead I use line anchors such as
"^[0-9-\.]+$"
"^[0-9-\.]+"
I get no matches and
"[0-9-\.]+$"
just matches the last number. So I am probably doing something wrong. Thanks for any help.
Um, pseudocode
Here’s an example using lots of stream magic: Split a string in C++?
Here’s an example using a vector:
Splitting a string by whitespace in c++
But plain old strtok is probably easiest:
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
in which case you’ll get something like
Now, that’s very C like because I’m out of practice for C++, but the key point here is that by trying to use regex, you make it overcomplicated.