0

MFC CString で Boost 正規表現を使用する際に問題が発生しました。正規表現は非常に単純です。文字列が探している dll の名前で終わっているかどうかを確認する必要があります。以下のコードでは、探している dll が CString パス DOES に含まれていますが、正規表現が失敗する理由がわかりません。ReleaseBuffer を使用するとバッファ サイズが増加するため、パスの長さは MAX_PATH に設定されます。なぜ正しくないか知っていますか?私は多くの試みをしましたが、常に失敗しました。

#include <boost/regex/mfc.hpp>
const CString ValuesDLLName = _T("MyDll.dll");
boost::tregex EndsWithRegex( _T(".+MyDll.dll\s*$") );

//boost::tregex EndsWithRegex1( _T("^.+Values\.dll\\s*$") );   // not working
//boost::tregex EndsWithRegex2( _T("^.+Values\.dll\s*$") );   // not working
//boost::tregex EndsWithRegex3( _T("^.+Values.dll\s*$") );   // not working
//boost::tregex EndsWithRegex4( _T("^.+Values.dll\\s*$") );   // not working
//boost::tregex EndsWithRegex5( _T("^.+Values\.dll\\s*$"),boost::regex::perl );   // not working
//boost::tregex EndsWithRegex6( _T("^.+Values\.dll\s*$"),boost::regex::perl );   // not working
//boost::tregex EndsWithRegex7( _T("^.+Values.dll\s*$"),boost::regex::perl );   // not working
//boost::tregex EndsWithRegex8( _T("^.+Values.dll\\s*$") ,boost::regex::perl);   // not working

CString Path;
boost::tmatch What;

_tsearchenv(ValuesDLLName, _T("PATH"), Path.GetBufferSetLength(260));
Path.ReleaseBuffer(260);

bool endsWithDllName = boost::regex_search( Path, What, EndsWithRegex );
4

1 に答える 1

1

C++ は最初のバックスラッシュをエスケープ文字として飲み込んでしまうため、バックスラッシュを 2 つにする必要があります。試す

boost::tregex EndsWithRegex( _T("^.+Values\\.dll\\s*$") );

ReleaseBufferまた、使い方も間違っていると思います。パラメータは、返された文字列の実際のサイズでなければなりません。そうしないと、文字列の末尾にゴミが含まれる可能性があります。文字列が null で終了することに依存できる場合は、パラメーターに常に -1 を使用するか、デフォルトであるため省略できます。

于 2010-09-21T22:11:38.597 に答える