Boost.Xpressive で検索位置を走査する
Boost.Spirit.Qi ではなく、Boost.Xpressive で。
sregex_iterator を使用します。
[ソース]
#include <boost/xpressive/xpressive.hpp> #include <functional> #include <string> int main(){ namespace x = boost::xpressive; using x::_; std::string source = " (homu) (mado) (mami)"; // () の位置と中身を全て列挙する x::sregex parser = "(" >> -*_ >> ")"; auto first = x::sregex_iterator(source.begin(), source.end(), parser); auto last = x::sregex_iterator(); std::for_each( first, last, [](x::smatch const& m){ std::cout << m.position() << std::endl; std::cout << m.str() << std::endl; } ); return 0; }
[出力]
2 (homu) 12 (mado) 19 (mami)
簡単な検索や置換なら Boost.Xpressive が簡単かな。