user2597359
user2597359

Reputation: 51

How to get array of matches

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

Answers (2)

user2637317
user2637317

Reputation:

What about using boost::sregex_iterator?

Upvotes: 0

ubi
ubi

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

Regular expression visualization

Edit live on Debuggex

Upvotes: 2

Related Questions