Is the range appropriate for regex loops?

In C ++ 11 draft drafts, a range-based loop can specify a range to iterate through a pair of iterators. This made it easy to repeat all matches for a regular expression. The ability to specify a range using a pair of iterators was later removed, and it is not in C ++ 11. Is there another easy way to iterate over all matches for a particular regular expression? I would like 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 any support of this type in C ++ 11?

+4
source share
2 answers

The problem with doing this for std::pair is that it "works" on a lot of things that are not valid ranges. Therefore, errors occur.

C ++ 11 does not have a built-in solution for this. You can use the Boost.Range make_iterator_range tool for easy creation . On the other hand, this is not entirely 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);} 
+7
source

You can still use a pair of iterators to indicate the sequence to iterate. The for(a: c) statement essentially repeats the sequence [begin(c), end(c)) . So all you have to do is use the match_results object or provide suitable begin and end functions that return one of the regular expression iterator types.

0
source

Source: https://habr.com/ru/post/1439063/


All Articles