0

我在使用带有 MFC CString 的 Boost 正则表达式时遇到问题。正则表达式非常简单:它必须检查字符串是否以我要查找的 dll 的名称结尾。在下面的代码中,CString 路径确实包含我正在寻找的 dll,但我不知道为什么正则表达式会失败。Uisng 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++ 会将第一个反斜杠作为转义字符吞噬。尝试

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

另外我认为你使用ReleaseBuffer不正确。该参数应该是返回的字符串的实际大小,否则字符串的末尾可能包含垃圾。如果您可以依赖以 null 结尾的字符串,则始终可以使用 -1 作为参数,或者将其省略,因为它是默认值。

于 2010-09-21T22:11:38.597 回答