五:regex_search学习
regex_search与regex_match基本相同,只不过regex_search不要求全部匹配,即部份匹配(查找)即可。
简单例子:
好了,再来一个例子,用于打印出所有的数字
Have digit:192
Have digit:168
Have digit:4
Have digit:1
regex_search与regex_match基本相同,只不过regex_search不要求全部匹配,即部份匹配(查找)即可。
简单例子:
std::string regstr = "(\\d+)";
boost::regex expression(regstr);
std::string testString = "192.168.4.1";
boost::smatch what;
if( boost::regex_search(testString, expression) )
{
std::cout<< "Have digit" << std::endl;
}
上面这个例子检测给出的字符串中是否包含数字。boost::regex expression(regstr);
std::string testString = "192.168.4.1";
boost::smatch what;
if( boost::regex_search(testString, expression) )
{
std::cout<< "Have digit" << std::endl;
}
好了,再来一个例子,用于打印出所有的数字
std::string regstr = "(\\d+)";
boost::regex expression(regstr);
std::string testString = "192.168.4.1";
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::cout<< "Have digit:" ;
std::string msg(what[1].first, what[1].second);
std::cout<< msg.c_str() << std::endl;
start = what[0].second;
}
打印出:boost::regex expression(regstr);
std::string testString = "192.168.4.1";
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::cout<< "Have digit:" ;
std::string msg(what[1].first, what[1].second);
std::cout<< msg.c_str() << std::endl;
start = what[0].second;
}
Have digit:192
Have digit:168
Have digit:4
Have digit:1