|
Boost Users : |
From: Lynn Allan (l_d_allan_at_[hidden])
Date: 2006-04-03 10:39:41
<alert comment="boost and regex newbie">
I'm using regex_search to detect which subexpression matched. I adapted the "Captures" example at:
http://www.boost.org/libs/regex/doc/captures.html
In the snippet below, it works ok to loop thru m[num].matched, but I was wondering if there was a more direct way of getting the information about which match was "hit". (The real application is going to have about 70 possible subexpressions to match against, instead of just four.)
std::string contents("String has two, three, one, and four in it. ");
boost::regex reg("(one)|(two)|(three)|(four)");
>From the above, I want the output to be:
<match=2>two
<match=3>three
<match=1>one
<match=4>four
void test_search()
{
boost::smatch m;
std::string contents("String has two, three, one, and four in it. ");
boost::regex reg("(one)|(two)|(three)|(four)");
std::string::const_iterator it = contents.begin();
std::string::const_iterator end = contents.end();
while (boost::regex_search(it, end, m, reg)) {
for (int num = 1; num <= 4; ++num) {
if (m[num].matched == true) {
std::cout << "<match=" << num << ">" << m[0] << std::endl;
break;
}
}
it = m[0].second;
}
}
</alert>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net