3

私はboost::regexを取得して、検索文字列内のパターンのすべての出現を取得しようとしています。そのようなことは簡単だと思いましたが、ブーストとSTLに任せて、すべての上にテンプレート難読化の10のメタレイヤーを追加します:)。

私の最近の試みはregex_search()を使用することですが、残念ながら私の呼び出しはどのオーバーロードとも一致していないようです。これが超蒸留された例です:

std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
while( regex_search( startPos, test.end(), testMatches, testPattern ) ) {
    // Do stuff: record match value, increment start position
}

regex_search()を呼び出すと、インテリセンスが作動し、コンパイルに失敗します(「'regex_search'のインスタンスが引数リストに一致しません」)。

私が呼び出そうとしているオーバーロードは次のとおりです。

template <class BidirectionalIterator,
    class Allocator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
    match_results<BidirectionalIterator, Allocator>& m,
    const basic_regex<charT, traits>& e,
    match_flag_type flags = match_default );

これは私の呼び出しとうまく一致しているようです。

どんなアイデアでも大歓迎です!この種のことを行うための代替方法と同様に。私が最終的にやりたいのは、次のような文字列を分割することです。

"0.11,0.22;0.33,0.444;0.555,0.666"

次に解析できるfloat文字列の構成リストに。

他の正規表現パッケージでは、単純です。「(?:([0-9。] +)[;,]?)+」のような式を実行すると、キャプチャされたグループに結果が含まれます。

4

2 に答える 2

4

問題は、実際にはイテレータタイプを混合していることです(std::string::iteratorおよびstd::string::const_iteratorregex_searchはテンプレート関数であるため、からiteratorへの暗黙的な変換const_iteratorは許可されていません。

testとして宣言するconst std::stringと修正され、の代わりにがtest.end()返されるというのは正しいことです。const_iteratoriterator

または、次の操作を実行できます。

std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
std::string::const_iterator endPos = test.end();
while( regex_search( startPos, endPos, testMatches, testPattern ) ) {
    // Do stuff: record match value, increment start position
}

std::string::cendC ++ 11を利用できる場合は、新しいメンバーを使用することもできます。

std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
while( regex_search( startPos, test.cend(), testMatches, testPattern ) ) {
    // Do stuff: record match value, increment start position
}
于 2013-03-25T20:16:35.233 に答える
0

OK、これを理解しました。検索された文字列が「const」と宣言されている場合、メソッドは正しく検出されます。例えば:

const std::string test = "1234567890";
于 2013-03-25T03:10:40.117 に答える