I am trying to parse some text using boost. i have looked at the example on the official site but it doesn’t seem to be working. I have written my own example; it doesn’t enter the while loop at all.
#include <boost/regex.hpp>
using namespace std;
int main(void)
{
string s;
string::const_iterator st, en;
s = "int_node:0 int_node:1 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:2 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:3 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:4 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:5 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:6 link_latency:1 bw_multiplier:16 link_weight:1\n"
"int_node:0 int_node:7 link_latency:1 bw_multiplier:16 link_weight:1";
st = s.begin();
en = s.end();
boost::regex expression("int_node:([0-9]+) int_node:([0-9]+)"
" link_latency:([0-9]+) bw_multiplier:([0-9]+)"
" link_weight:([0-9]+) long_range:([0-9]+)"
" no_of_vcs:([0-9]+)");
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while(boost::regex_search(st, en, what, expression, flags))
{
st = what[0].second;
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
cout << what[1] << " " << what[2] << " " << what[3] << "\n";
}
return 0;
}
I am trying to parse a file that looks like this:
int_node:0 int_node:1 link_latency:1 bw_multiplier:16 link_weight:1
int_node:0 int_node:2 link_latency:1 bw_multiplier:16 link_weight:1
int_node:0 int_node:3 link_latency:1 bw_multiplier:16 link_weight:1
int_node:0 int_node:4 link_latency:1 bw_multiplier:16 link_weight:1
and retrieve the integer values for each field.
Your regular expression has 7 elements but each line you’re searching only has 5.
You could try one of two options. Either reduce the number of elements to only 5
or, if there will be some lines that might have the last two elements (and you want to reference them), use a positive look-ahead assertion like this:
If you’re not familiar with regex, the
(?:REGEX)?will say that the last two elements are optional, and the?:at the beginning means this particular grouping doesn’t count. If there is a value in the 6th column (long_range), thenwhat[6]will contain the long_range value, otherwise it will be blank.