| title | regex_iterator Class | Microsoft Docs | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ms.custom | ||||||||||||||
| ms.date | 11/04/2016 | |||||||||||||
| ms.reviewer | ||||||||||||||
| ms.suite | ||||||||||||||
| ms.technology |
|
|||||||||||||
| ms.tgt_pltfrm | ||||||||||||||
| ms.topic | article | |||||||||||||
| f1_keywords |
|
|||||||||||||
| dev_langs |
|
|||||||||||||
| helpviewer_keywords |
|
|||||||||||||
| ms.assetid | 0cfd8fd0-5a95-4f3c-bf8e-6ef028c423d3 | |||||||||||||
| caps.latest.revision | 16 | |||||||||||||
| author | corob-msft | |||||||||||||
| ms.author | corob | |||||||||||||
| manager | ghogen | |||||||||||||
| translation.priority.mt |
|
Iterator class for matches.
template<class BidIt,
class Elem = typename std::iterator_traits<BidIt>::value_type,
class RxTraits = regex_traits<Elem> >
class regex_iterator {
public:
typedef basic_regex<Elem, RXtraits> regex_type;
typedef match_results<BidIt> value_type;
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef const match_results<BidIt>* pointer;
typedef const match_results<BidIt>& reference;
regex_iterator();
regex_iterator(
BidIt first, BidIt last, const regex_type& re,
regex_constants::match_flag_type f = regex_constants::match_default);
bool operator==(const regex_iterator& right);
bool operator!=(const regex_iterator& right);
const match_results<BidIt>& operator*();
const match_results<BidIt> * operator->();
regex_iterator& operator++();
regex_iterator& operator++(int);
private:
BidIt begin; // exposition only
BidIt end; // exposition only
regex_type *pregex; // exposition only
regex_constants::match_flag_type flags; // exposition only
match_results<BidIt> match; // exposition only
};
BidIt
The iterator type for submatches.
Elem
The type of elements to match.
RXtraits
Traits class for elements.
The template class describes a constant forward iterator object. It extracts objects of type match_results<BidIt> by repeatedly applying its regular expression object *pregex to the character sequence defined by the iterator range [begin, end).
See the following topics for examples on regular expressions:
Header: <regex>
Namespace: std
The type of an iterator difference.
typedef std::ptrdiff_t difference_type;
The type is a synonym for std::ptrdiff_t.
// std__regex__regex_iterator_difference_type.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
The type of the iterator category.
typedef std::forward_iterator_tag iterator_category;
The type is a synonym for std::forward_iterator_tag.
// std__regex__regex_iterator_iterator_category.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
Compares iterators for inequality.
bool operator!=(const regex_iterator& right);
right
The iterator to compare to.
The member function returns !(*this == right).
// std__regex__regex_iterator_operator_ne.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
Accesses the designated match.
const match_results<BidIt>& operator*();
The member function returns the stored value match.
// std__regex__regex_iterator_operator_star.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
Increments the iterator.
regex_iterator& operator++();
regex_iterator& operator++(int);
If the current match has no characters the first operator calls regex_search(begin, end, match, *pregex, flags | regex_constants::match_prev_avail | regex_constants::match_not_null); otherwise it advances the stored value begin to point to the first character after the current match then calls regex_search(begin, end, match, *pregex, flags | regex_constants::match_prev_avail). In either case, if the search fails the operator sets the object to an end-of-sequence iterator. The operator returns the object.
The second operator makes a copy of the object, increments the object, then returns the copy.
// std__regex__regex_iterator_operator_inc.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
Compares iterators for equality.
bool operator==(const regex_iterator& right);
right
The iterator to compare to.
The member function returns true if *this and right are both end-of-sequence iterators or if neither is an end-of-sequence iterator and begin == right.begin, end == right.end, pregex == right.pregex, and flags == right.flags. Otherwise it returns false.
// std__regex__regex_iterator_operator_as.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
Accesses the designated match.
const match_results<BidIt> * operator->();
The member function returns the address of the stored value match.
// std__regex__regex_iterator_operator_arrow.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
The type of a pointer to a match.
typedef match_results<BidIt> *pointer;
The type is a synonym for match_results<BidIt>*, where BidIt is the template parameter.
// std__regex__regex_iterator_pointer.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
} match == a
match == a
match == a
The type of a reference to a match.
typedef match_results<BidIt>& reference;
The type is a synonym for match_results<BidIt>&, where BidIt is the template parameter.
// std__regex__regex_iterator_reference.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
}
match == a
match == a
match == a
Constructs the iterator.
regex_iterator();
regex_iterator(BidIt first,
BidIt last,
const regex_type& re,
regex_constants::match_flag_type f = regex_constants::match_default);
first
Beginning of sequence to match.
last
End of sequence to match.
re
Regular expression for matches.
f
Flags for matches.
The first constructor constructs an end-of-sequence iterator. The second constructor initializes the stored value begin with first, the stored value end with last, the stored value pregex with &re, and the stored value flags with f. It then calls regex_search(begin, end, match, *pregex, flags). If the search fails, the constructor sets the object to an end-of-sequence iterator.
// std__regex__regex_iterator_construct.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
}
match == a
match == a
match == a
The type of the regular expression to match.
typedef basic_regex<Elem, RXtraits> regex_type;
The typedef is a synonym for basic_regex<Elem, RXtraits>.
// std__regex__regex_iterator_regex_type.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
}
match == a
match == a
match == a
The type of a match.
typedef match_results<BidIt> value_type;
The type is a synonym for match_results<BidIt>, where BidIt is the template parameter.
// std__regex__regex_iterator_value_type.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
for (; next != end; ++next)
std::cout << "match == " << next->str() << std::endl;
// other members
Myiter it1(pat, pat + strlen(pat), rx);
Myiter it2(it1);
next = it1;
Myiter::iterator_category cat = std::forward_iterator_tag();
Myiter::difference_type dif = -3;
Myiter::value_type mr = *it1;
Myiter::reference ref = mr;
Myiter::pointer ptr = &ref;
dif = dif; // to quiet "unused" warnings
ptr = ptr;
return (0);
}
match == a
match == a
match == a
<regex>
regex_constants Class
regex_error Class
<regex> functions
regex_iterator Class
<regex> operators
regex_token_iterator Class
regex_traits Class
<regex> typedefs