首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::regex_token_iterator

Defined in header <regex>

template< class BidirIt, class CharT = typename std::iterator_traits<BidirIt>::value_type, class Traits = std::regex_traits<CharT> > class regex_token_iterator

(since C++11)

std::regex_token_iterator是只读ForwardIterator它访问底层字符序列中正则表达式的每个匹配项的单个子匹配。它还可以用于访问序列中与给定正则表达式%28不匹配的部分。作为标记器%29。

在构造上,它构造了一个std::regex_iterator在每一个增量上,它都会从当前匹配中逐步遍历请求的子匹配。[医]结果,递增基础regex_iterator当从上一次子匹配中递增时。

默认构造std::regex_token_iterator是序列结束迭代器。当一个有效的std::regex_token_iterator在达到最后一次匹配的最后一个子匹配后,它就等于序列结束迭代器。解除引用或增量将进一步调用未定义的行为。

在成为序列结束迭代器之前,std::regex_token_iterator可能成为后缀迭代器,如果索引-1%28非匹配片段%29出现在请求的子匹配索引列表中。这样的迭代器,如果取消引用,则返回匹配。[医]结果与最后一次匹配到序列结束之间的字符序列相对应。

的典型实现std::regex_token_iterator持有底层std::regex_iterator、容器%28等。std::vector<int>请求的子匹配索引的%29,内部计数器等于子匹配的索引,指向std::sub_match,指向当前匹配的当前子匹配,以及std::match_results对象,该对象包含在令牌器模式%29中使用的最后一个不匹配字符序列%28。

类型要求

-Bidirit必须符合双向迭代器的要求。

*。

专门性

定义了几种常见字符序列类型的专门化:

在标头中定义<regex>

*。

类型定义

克雷格[医]令牌[医]迭代器正则表达式[医]令牌[医]迭代器<Const char%2A>

冬青[医]令牌[医]迭代器正则表达式[医]令牌[医]迭代器<constwchar[医]T型%2A>

瑞吉[医]令牌[医]迭代器正则表达式[医]令牌[医]迭代器<std::string::const[医]迭代器>

瑞吉[医]令牌[医]迭代器正则表达式[医]令牌[医]迭代器<std::wstring::const[医]迭代器>

成员类型

Member type

Definition

value_type

std::sub_match<BidirIt>

difference_type

std::ptrdiff_t

pointer

const value_type*

reference

const value_type&

iterator_category

std::forward_iterator_tag

regex_type

basic_regex<CharT, Traits>

成员函数

(constructor)

constructs a new regex_token_iterator (public member function)

(destructor) (implicitly declared)

destructs a regex_token_iterator, including the cached value (public member function)

operator=

assigns contents (public member function)

operator==operator!=

compares two regex_token_iterators (public member function)

operator*operator->

accesses current submatch (public member function)

operator++operator++(int)

advances the iterator to the next submatch (public member function)

注记

程序员%27的责任是确保std::basic_regex对象传递给迭代器%27s构造函数的。因为迭代器存储std::regex_iterator它存储一个指向正则表达式的指针,在regex被销毁后增加迭代器,结果会导致未定义的行为。

二次

代码语言:javascript
复制
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
 
int main()
{
   std::string text = "Quick brown fox.";
   // tokenization (non-matched fragments)
   // Note that regex is matched only two times: when the third value is obtained
   // the iterator is a suffix iterator.
   std::regex ws_re("\\s+"); // whitespace
   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"));
 
   // iterating the first submatches
   std::string html = "<p><a href=\"http://google.com\">google</a> "
                      "< a HREF =\"http://cppreference.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"));
}

二次

产出:

二次

代码语言:javascript
复制
Quick
brown
fox.
http://google.com
http://cppreference.com

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券