regex_token_iterator.qbk
来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 432 行 · 第 1/2 页
QBK
432 行
[*Postconditions]: `*this == that`.[#boost_regex.regex_token_iterator.op_eq] bool operator==(const regex_token_iterator&)const;[*Effects]: returns true if `*this` is the same position as `that`.[#boost_regex.regex_token_iterator.op_ne] bool operator!=(const regex_token_iterator&)const;[*Effects]: returns `!(*this == that)`.[#boost_regex.regex_token_iterator.op_deref] const value_type& operator*()const;[*Effects]: returns the current character sequence being enumerated.[#boost_regex.regex_token_iterator.op_arrow] const value_type* operator->()const;[*Effects]: returns `&(*this)`.[#boost_regex.regex_token_iterator.op_inc1] regex_token_iterator& operator++();[*Effects]: Moves on to the next character sequence to be enumerated.[*Throws]: `std::runtime_error` if the complexity of matching the expression against an N character string begins to exceed O(N[super 2]), or if the program runs out of stack space while matching the expression (if Boost.Regex is configured in recursive mode), or if the matcher exhausts its permitted memory allocation (if Boost.Regex is configured in non-recursive mode).[*Returns]: `*this`.[#boost_regex.regex_token_iterator.op_inc2] regex_token_iterator& operator++(int);[*Effects]: constructs a copy result of `*this`, then calls `++(*this)`.[*Returns]: result.[#boost_regex.regex_token_iterator.make] template <class charT, class traits> regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator( const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default); template <class charT, class traits, class ST, class SA> regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator( const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default); template <class charT, class traits, std::size_t N> regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator( const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default); template <class charT, class traits, class ST, class SA, std::size_t N> regex_token_iterator< typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator( const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default); template <class charT, class traits> regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator( const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default); template <class charT, class traits, class ST, class SA> regex_token_iterator< typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator( const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default);[*Effects]: returns a [regex_token_iterator] that enumerates one [sub_match] for each value in /submatch/ for each occurrence of regular expression /e/ in string /p/, matched using [match_flag_type] /m/.[h4 Examples]The following example takes a string and splits it into a series of tokens: #include <iostream> #include <boost/regex.hpp> using namespace std; int main(int argc) { string s; do{ if(argc == 1) { cout << "Enter text to split (or \"quit\" to exit): "; getline(cin, s); if(s == "quit") break; } else s = "This is a string of tokens"; boost::regex re("\\s+"); boost::sregex_token_iterator i(s.begin(), s.end(), re, -1); boost::sregex_token_iterator j; unsigned count = 0; while(i != j) { cout << *i++ << endl; count++; } cout << "There were " << count << " tokens found." << endl; }while(argc == 1); return 0; }The following example takes a html file and outputs a list of all the linked files: #include <fstream> #include <iostream> #include <iterator> #include <boost/regex.hpp> boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", boost::regex::normal | boost::regbase::icase); void load_file(std::string& s, std::istream& is) { s.erase(); // // attempt to grow string buffer to match file size, // this doesn't always work... s.reserve(is.rdbuf()->in_avail()); char c; while(is.get(c)) { // use logarithmic growth stategy, in case // in_avail (above) returned zero: if(s.capacity() == s.size()) s.reserve(s.capacity() * 3); s.append(1, c); } } int main(int argc, char** argv) { std::string s; int i; for(i = 1; i < argc; ++i) { std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; s.erase(); std::ifstream is(argv[i]); load_file(s, is); boost::sregex_token_iterator i(s.begin(), s.end(), e, 1); boost::sregex_token_iterator j; while(i != j) { std::cout << *i++ << std::endl; } } // // alternative method: // test the array-literal constructor, and split out the whole // match as well as $1.... // for(i = 1; i < argc; ++i) { std::cout << "Findings URL's in " << argv[i] << ":" << std::endl; s.erase(); std::ifstream is(argv[i]); load_file(s, is); const int subs[] = {1, 0,}; boost::sregex_token_iterator i(s.begin(), s.end(), e, subs); boost::sregex_token_iterator j; while(i != j) { std::cout << *i++ << std::endl; } } return 0; } [endsect]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?