六:关于重复的贪婪
我们先来一个例子:
我们希望得到的是打印人名,然后打印年龄。但是效果令我们大失所望:
Name:My age is 28 His
Age:27
嗯,查找原因:这是由于"+"号或者"*"号等重复符号带来的副作用,这些符号会消耗尽可能多的输入,使之是“贪婪”的。即正则表达式(.*)会匹配最长的串,而不是匹配最短的成功串。
如何使得这些重复的符号不再“贪婪”,我们在重复符号后加上"?"即可。
Name:My
Age:28
Name: His
Age:27
我们先来一个例子:
std::string regstr = "(.*)(age)(.*)(\\d{2})";
boost::regex expression(regstr);
std::string testString = "My age is 28 His age is 27";
boost::smatch what;
std::string::const_iterator start = testString.begin();
std::string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{
std::string name(what[1].first, what[1].second);
std::string age(what[4].first, what[4].second);
std::cout<< "Name:" << name.c_str() << std::endl;
std::cout<< "Age:" <<age.c_str() << std::endl;
start = what[0].second;
}
boost::regex expression(regstr);
std::string testString = "My age is 28 His age is 27";
boost::smatch what;
std::string::const_iterator start = testString.begin();
std::string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{
std::string name(what[1].first, what[1].second);
std::string age(what[4].first, what[4].second);
std::cout<< "Name:" << name.c_str() << std::endl;
std::cout<< "Age:" <<age.c_str() << std::endl;
start = what[0].second;
}
我们希望得到的是打印人名,然后打印年龄。但是效果令我们大失所望:
Name:My age is 28 His
Age:27
嗯,查找原因:这是由于"+"号或者"*"号等重复符号带来的副作用,这些符号会消耗尽可能多的输入,使之是“贪婪”的。即正则表达式(.*)会匹配最长的串,而不是匹配最短的成功串。
如何使得这些重复的符号不再“贪婪”,我们在重复符号后加上"?"即可。
std::string regstr = "(.*?)(age)(.*?)(\\d{2})";
boost::regex expression(regstr);
std::string testString = "My age is 28 His age is 27";
boost::smatch what;
std::string::const_iterator start = testString.begin();
std::string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{
std::string name(what[1].first, what[1].second);
std::string age(what[4].first, what[4].second);
std::cout<< "Name:" << name.c_str() << std::endl;
std::cout<< "Age:" <<age.c_str() << std::endl;
start = what[0].second;
}
打印输出:boost::regex expression(regstr);
std::string testString = "My age is 28 His age is 27";
boost::smatch what;
std::string::const_iterator start = testString.begin();
std::string::const_iterator end = testString.end();
while( boost::regex_search(start, end, what, expression) )
{
std::string name(what[1].first, what[1].second);
std::string age(what[4].first, what[4].second);
std::cout<< "Name:" << name.c_str() << std::endl;
std::cout<< "Age:" <<age.c_str() << std::endl;
start = what[0].second;
}
Name:My
Age:28
Name: His
Age:27