1. 程式人生 > >[Boost]_[使用boost正則表示式查詢所有字串]

[Boost]_[使用boost正則表示式查詢所有字串]

場景:

1.比如在html中查找出現過的<img>標籤,好提取圖片地址.

2.編譯器不支援C++11標準時使用boost庫是比較好的選擇.

test_search.cpp

#include <stdio.h>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
#include <iostream> 

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<std::string::const_iterator> 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


參考: