当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ regex_iterator()用法及代码示例


regex_iterator()是C++中BiDirectionalIterator类的函数。此方法返回一个迭代器类型,以迭代序列中同一正则表达式模式的不同匹配。

用法:

template<
    class BidirectionalIterator,
    class CharT = typename std::iterator_traits::value_type,
    class Traits = std::regex_traits > class regex_iterator

C++ 14语法


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

C++ 11语法:

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

参数:此方法接受以下参数:

  • BidirectionalIterator:迭代目标字符序列。
  • 字符:这是一个字符类型。
  • traits:这是正则表达式特征类型。

返回值:此方法返回带有结果序列的字符串对象。

以下示例说明了regex_iterator()方法:

例:

#include <iostream> 
#include <iterator> 
#include <regex> 
#include <string> 
using namespace std; 
  
int main() 
{ 
    const string 
        strg 
        = "Geeksforgeeks welcome geeks."; 
  
    regex words_regex("[^\\s]+"); 
    auto
        words_begin 
        = sregex_iterator( 
            strg.begin(), 
            strg.end(), 
            words_regex); 
  
    auto words_end = sregex_iterator(); 
  
    cout << "Trying to find words"
         << " using regex_iterator:\n\n"; 
  
    cout << "Number of words found:"
         << distance(words_begin, words_end); 
  
    cout << "\n\nThe words are:\n"; 
    for (sregex_iterator k = words_begin; 
         k != words_end; 
         ++k) { 
  
        smatch match = *k; 
        string match_str = match.str(); 
  
        cout << match_str 
             << endl; 
    } 
}
输出:
Trying to find words using regex_iterator:

Number of words found:3

The words are:
Geeksforgeeks
welcome
geeks.


相关用法


注:本文由纯净天空筛选整理自Pushpanjali chauhan大神的英文原创作品 regex_iterator() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。