當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。