# 31 Regular expressions library [re]

## 31.12 Regular expression iterators [re.iter]

### 31.12.1 Class template regex_­iterator[re.regiter]

The class template regex_­iterator is an iterator adaptor. It represents a new view of an existing iterator sequence, by enumerating all the occurrences of a regular expression within that sequence. A regex_­iterator uses regex_­search to find successive regular expression matches within the sequence from which it was constructed. After the iterator is constructed, and every time operator++ is used, the iterator finds and stores a value of match_­results<BidirectionalIterator>. If the end of the sequence is reached (regex_­search returns false), the iterator becomes equal to the end-of-sequence iterator value. The default constructor constructs an end-of-sequence iterator object, which is the only legitimate iterator to be used for the end condition. The result of operator* on an end-of-sequence iterator is not defined. For any other iterator value a const match_­results<BidirectionalIterator>& is returned. The result of operator-> on an end-of-sequence iterator is not defined. For any other iterator value a const match_­results<BidirectionalIterator>* is returned. It is impossible to store things into regex_­iterators. Two end-of-sequence iterators are always equal. An end-of-sequence iterator is not equal to a non-end-of-sequence iterator. Two non-end-of-sequence iterators are equal when they are constructed from the same arguments.

```namespace std {
template <class BidirectionalIterator,
class charT = typename iterator_traits<BidirectionalIterator>::value_type,
class traits = regex_traits<charT>>
class regex_iterator {
public:
using regex_type        = basic_regex<charT, traits>;
using iterator_category = forward_iterator_tag;
using value_type        = match_results<BidirectionalIterator>;
using difference_type   = ptrdiff_t;
using pointer           = const value_type*;
using reference         = const value_type&;

regex_iterator();
regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
const regex_type& re,
regex_constants::match_flag_type m = regex_constants::match_default);
regex_iterator(BidirectionalIterator, BidirectionalIterator,
const regex_type&&,
regex_constants::match_flag_type = regex_constants::match_default) = delete;
regex_iterator(const regex_iterator&);
regex_iterator& operator=(const regex_iterator&);
bool operator==(const regex_iterator&) const;
bool operator!=(const regex_iterator&) const;
const value_type& operator*() const;
const value_type* operator->() const;
regex_iterator& operator++();
regex_iterator operator++(int);

private:
BidirectionalIterator                begin;  // exposition only
BidirectionalIterator                end;    // exposition only
const regex_type*                    pregex; // exposition only
regex_constants::match_flag_type     flags;  // exposition only
match_results<BidirectionalIterator> match;  // exposition only
};
}```

An object of type regex_­iterator that is not an end-of-sequence iterator holds a zero-length match if match.matched == true and match.first == match.second. [Note: For example, this can occur when the part of the regular expression that matched consists only of an assertion (such as `'^'`, `'\$'`, '\b', '\B'). end note]

#### 31.12.1.1regex_­iterator constructors [re.regiter.cnstr]

```regex_iterator(); ```

Effects: Constructs an end-of-sequence iterator.

```regex_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, regex_constants::match_flag_type m = regex_constants::match_default); ```

Effects: Initializes begin and end to a and b, respectively, sets pregex to &re, sets flags to m, then calls regex_­search(begin, end, match, *pregex, flags). If this call returns false the constructor sets *this to the end-of-sequence iterator.

#### 31.12.1.2regex_­iterator comparisons [re.regiter.comp]

```bool operator==(const regex_iterator& right) const; ```

Returns: true if *this and right are both end-of-sequence iterators or if the following conditions all hold:

• begin == right.begin,

• end == right.end,

• pregex == right.pregex,

• flags == right.flags, and

• match == right.match;

otherwise false.

```bool operator!=(const regex_iterator& right) const; ```

Returns: !(*this == right).

#### 31.12.1.3regex_­iterator indirection [re.regiter.deref]

```const value_type& operator*() const; ```

Returns: match.

```const value_type* operator->() const; ```

Returns: &match.

#### 31.12.1.4regex_­iterator increment [re.regiter.incr]

```regex_iterator& operator++(); ```

Effects: Constructs a local variable start of type BidirectionalIterator and initializes it with the value of match.second.

If the iterator holds a zero-length match and start == end the operator sets *this to the end-of-sequence iterator and returns *this.

Otherwise, if the iterator holds a zero-length match, the operator calls:

```regex_search(start, end, match, *pregex,
flags | regex_constants::match_not_null | regex_constants::match_continuous)```

If the call returns true the operator returns *this. Otherwise the operator increments start and continues as if the most recent match was not a zero-length match.

If the most recent match was not a zero-length match, the operator sets flags to flags | regex_­constants​::​match_­prev_­avail and calls regex_­search(start, end, match, *pregex, flags). If the call returns false the iterator sets *this to the end-of-sequence iterator. The iterator then returns *this.

In all cases in which the call to regex_­search returns true, match.prefix().first shall be equal to the previous value of match.second, and for each index i in the half-open range [0, match.size()) for which match[i].matched is true, match.position(i) shall return distance(begin, match[i].​first).

[Note: This means that match.position(i) gives the offset from the beginning of the target sequence, which is often not the same as the offset from the sequence passed in the call to regex_­search. end note]

It is unspecified how the implementation makes these adjustments.

[Note: This means that a compiler may call an implementation-specific search function, in which case a user-defined specialization of regex_­search will not be called. end note]

```regex_iterator operator++(int); ```

Effects: As if by:

```regex_iterator tmp = *this;
++(*this);
return tmp;```
{"ft_lang_label":"__label__en","ft_lang_prob":0.66599077,"math_prob":0.93853724,"size":5086,"snap":"2021-04-2021-17","text_gpt3_token_len":1060,"char_repetition_ratio":0.23632428,"word_repetition_ratio":0.10742496,"special_character_ratio":0.2152969,"punctuation_ratio":0.1678744,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9595874,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-10T18:32:52Z\",\"WARC-Record-ID\":\"<urn:uuid:2e41a087-826e-433e-8cff-7e1ddc6edf4b>\",\"Content-Length\":\"20773\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bb5b28f6-4aa0-4676-bc5b-f1794b4ee646>\",\"WARC-Concurrent-To\":\"<urn:uuid:78e9877e-227e-4cf6-a679-852451a8cf6a>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"https://timsong-cpp.github.io/cppwp/n4659/re.regiter\",\"WARC-Payload-Digest\":\"sha1:4XEOKWHWXXH3X3PXQVLZ4ZU5RIIWJLQY\",\"WARC-Block-Digest\":\"sha1:G5TB6D4GYSOWFNT7EL5VYSADOZLFWCN7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038057476.6_warc_CC-MAIN-20210410181215-20210410211215-00088.warc.gz\"}"}