Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range-based for-loop over regex matches?

Tags:

c++

c++11

In pre-final drafts of C++11, a range-based for loop could specify the range to iterate over via a pair of iterators. This made it easy to iterate over all matches for a regular expression. The ability to specify a range using a pair of iterators was later removed, and it is not present in C++11. Is there still a straightforward way to iterate over all matches for a particular regular expression? I'd like to be able to do something like this:

std::regex begin(" 1?2?3?4* ");
std::regex end;

for(auto& match: std::pair(begin, end)) process(*match);

Is there support for this kind of thing in C++11?

like image 982
KnowItAllWannabe Avatar asked Feb 18 '23 21:02

KnowItAllWannabe


1 Answers

The problem with doing it for std::pair is that it "works" on a lot of things that aren't valid ranges. Thus causing errors.

C++11 doesn't come with a built-in solution for this. You can use Boost.Range's make_iterator_range facility to build one easily. Then again, it's not exactly difficult to do manually:

template<typename T>
class IterRange
{
  T start;
  T end;
public:
  IterRange(const T &start_, const T &end_) : start(start_), end(end_) {}

  T begin() {return start;}
  T end() {return end;}
};

template<typename T> IterRange<T> make_range(const T &start, const T &end) {return IterRange<T>(start, end);}
like image 199
Nicol Bolas Avatar answered Feb 26 '23 20:02

Nicol Bolas