Reputation: 51
I'm trying to do something like in the example of boost::regex_serach in here .
My sample of code is :
boost::regex expression("<name=\"[a-zA-z0-9]+\" "
"init=\"[0-9]+\"/>");
std::string::const_iterator start, end;
start = Field.begin();
end = Field.end();
boost::smatch what;
boost::match_flag_type flags = boost::match_default;
while(regex_search(start, end, what, expression,flags)){
std::cout<<"@@@" << std::string(what[0].first,what[0].second);
start = what[0].second;
// update flags:
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
I have some xml file ..... name="SamleName" init="6"/...
And want to have SamlpeName in what[1]
and init value in what[2]
, but code listed here only write this field" name="SamleName" init="6"/"
to what[0]
.
How can I split this like in regex_search example ??
Upvotes: 2
Views: 524
Reputation: 4399
From a purely regex point of view (I haven't used boost regex library) you need to group the required matches with round brackets ()
. Refer to http://www.regular-expressions.info/brackets.html for more details.
So your regex should be something like this
boost::regex expression("<name=(\"[a-zA-z0-9]+\") init=(\"[0-9]+\")/>");
You can visualize the matching like this
<name=(\"[a-zA-z0-9]+\") init=(\"[0-9]+\")/>
Upvotes: 2