boostorg / boostorg/regex

When using match_extra with regex_iterator captures from previous iterations are incorrectly preserved

Open
#147 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
119
Forks
113
PR merge metrics
No merged PRs in 30d

Description

boost::regex has a **wonderful** feature boost::regex_constants::match_extra. It works well.
But when using regex_iterator the contents of the captures() collection is not cleared before running next regex_search.
regex_iterator_implementation::next() should clear all captures (capture_sequence_type) before invoking next regex_search.
(( As a workaround get_captures() method can be used to clear captures for all submatches from the calling code on each iteration))

To demonstate the problem the following code will unexpectedly output
123456321789123654
instead of
123456789

```
#include
#include
#define BOOST_REGEX_MATCH_EXTRA
#include

int main()
{

std::string str = "BEG123END BEG456END BEG789END";
boost::regex r("BEG(?:(?\\d))+END");

boost::sregex_iterator beg(str.cbegin(), str.cend(), r, boost::regex_constants::match_extra);
for (boost::sregex_iterator i = beg; i != boost::sregex_iterator(); ++i)
{
const boost::ssub_match::capture_sequence_type& captures = (*i)["group"].captures();
for (const boost::ssub_match& c : captures)
{
std::cout << c.str();
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.