2

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 ??

ubi
  • 4,041
  • 3
  • 33
  • 50
  • I think you may want to mark your expression with subexpressions ` ` - notice the additional `( )` , via them you can access them in most regex implementation ( dunno for boost actually, but I belive they did that as well ) – Najzero Aug 02 '13 at 07:33

2 Answers2

2

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]+\")/>

Regular expression visualization

Edit live on Debuggex

Community
  • 1
  • 1
ubi
  • 4,041
  • 3
  • 33
  • 50
0

What about using boost::sregex_iterator?