C++ Regex Library - regex_token_iterator



Description

It is a regex token iterator.

Declaration

Following is the declaration for 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;

Parameters

  • BidirectionalIterator − It is a bidirectional iterator type that iterates on the target sequence of characters.

  • charT − It is a char type.

  • traits − It is a regex traits type.

Return Value

It returns a string object with the resulting sequence.

Exceptions

No-noexcept − this member function never throws exceptions.

Example

In below example for 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"));
}

The output should be like this −

Tutorialspoint
india
pvt
ltd.
http://tutorialspoint.com
http://indiabbc.com
regex.htm
Advertisements