8

データストリーム内の興味深いデータの場合、いくつかのチャンクを一致させようとしています。

先頭<に 4 文字の英数字、2 文字のチェックサム (または??シェックサムが指定されていない場合)、および末尾の>.

最後の 2 文字が英数字の場合、次のコードは期待どおりに機能します。もしそうなら??、それは失敗します。

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data??>bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

ドキュメントには、これが当てはまることを示唆するものは何も見つかりませんでした(NULLと改行以外はすべてAIUIと一致する必要があります)。

それで、私は何を逃したのですか?

4

1 に答える 1

10

??>trigraphであるため、 に変換され}ます。コードは次と同等です。

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data}bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

これに変更できます:

std::string haystack = "Fli<data?" "?>bble";

デモ(注:std::regex多かれ少なかれ同じものを使用します)

注: trigraph は C++11 から非推奨になり、(おそらく) C++17 から削除されます

于 2016-11-08T10:00:53.210 に答える