C++ 正则表达式库 - regex_token_iterator



描述

这是一个正则表达式标记迭代器。

声明

以下是 std::regex_token_iterator 的声明。

template <class BidirectionalIterator,
          class charT=typename iterator_traits<BidirectionalIterator>::value_type,
          class traits=regex_traits<charT> > class regex_token_iterator;

C++11

template <class BidirectionalIterator,
          class charT=typename iterator_traits<BidirectionalIterator>::value_type,
          class traits=regex_traits<charT> > class regex_token_iterator;

C++14

template <class BidirectionalIterator,
          class charT=typename iterator_traits<BidirectionalIterator>::value_type,
          class traits=regex_traits<charT> > class regex_token_iterator;

参数

  • BidirectionalIterator − 这是一个双向迭代器类型,用于迭代目标字符序列。

  • charT − 这是一个字符类型。

  • traits − 这是一个正则表达式特性类型。

返回值

它返回一个包含结果序列的字符串对象。

异常

No-noexcept − 此成员函数从不抛出异常。

示例

以下为 std::regex_token_iterator 的示例。

#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
 
int main() {
   std::string text = "Tutorialspoint india pvt ltd.";

   std::regex ws_re("\\s+"); 
   std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
      std::sregex_token_iterator(),
      std::ostream_iterator<std::string>(std::cout, "\n"));

   std::string html = "<p><a href=\"https://tutorialspoint.com\">google</a> "
      "< a HREF =\"http://indiabbc.com\">cppreference</a>\n</p>";
   std::regex url_re("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", std::regex::icase);
   std::copy( std::sregex_token_iterator(html.begin(), html.end(), url_re, 1),
      std::sregex_token_iterator(),
      std::ostream_iterator<std::string>(std::cout, "\n"));
}

输出应如下所示:

Tutorialspoint
india
pvt
ltd.
https://tutorialspoint.com
http://indiabbc.com
regex.htm
广告
© . All rights reserved.