Bibliothèque Regex C ++ - regex_token_iterator

La description

C'est un itérateur de jeton regex.

Déclaration

Voici la déclaration pour 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;

Paramètres

  • BidirectionalIterator - C'est un type d'itérateur bidirectionnel qui itère sur la séquence cible de caractères.

  • charT - C'est un type char.

  • traits - C'est un type de traits regex.

Valeur de retour

Il renvoie un objet chaîne avec la séquence résultante.

Exceptions

No-noexcept - cette fonction membre ne lève jamais d'exceptions.

Exemple

Dans l'exemple ci-dessous pour 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=\"http://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"));
}

La sortie devrait être comme ça -

Tutorialspoint
india
pvt
ltd.
http://tutorialspoint.com
http://indiabbc.com