[Boost]_[使用boost正则表达式查找所有字符串]


场景:

1.比如在html中查找出现过的标签,好提取图片地址.

2.编译器不支持C++11标准时使用boost库是比较好的选择.


test_search.cpp

#include 
#include 
#include 
#include 
#include  

using namespace std;

int main(int argc, char *argv[])
{
	std::string str("infoasdf123unil98oasdfa686^&*(432");
	boost::regex e1("[0-9]+");

	std::string::const_iterator start, end;
	start = str.begin();
	end = str.end();

	boost::match_results what;
    boost::match_flag_type flags = boost::match_default;

    while(regex_search(start, end, what, e1, flags)) 
    {	
    	cout << "finded number: " << string(what[0].first,what[0].second) << endl;
    	start = what[0].second;
    }
	return 0;
}

//http://www.boost.org/doc/libs/1_46_1/libs/regex/doc/html/boost_regex/ref/regex_search.html

输出:

finded number: 123
finded number: 98
finded number: 686
finded number: 432



参考:

http://www.boost.org/doc/libs/1_46_1/libs/regex/doc/html/boost_regex/ref/regex_search.html

你可能感兴趣的:(系统平台)