boost正则表达式搜索失败,带有MFC CString

boost正则表达式搜索失败,带有MFC CString,mfc,cstring,boost-regex,Mfc,Cstring,Boost Regex,我在使用带有MFC字符串的Boost正则表达式时遇到了一个问题。 正则表达式非常简单:它必须检查字符串是否以我要查找的dll的名称结尾。 在下面的代码中,CString路径确实包含我要查找的dll,但我不知道正则表达式失败的原因。使用ReleaseBuffer会增加缓冲区大小,以便将路径长度设置为MAX_Path。 你知道为什么不正确吗? 我做了很多尝试,但总是失败 #include <boost/regex/mfc.hpp> const CString ValuesDLLName

我在使用带有MFC字符串的Boost正则表达式时遇到了一个问题。 正则表达式非常简单:它必须检查字符串是否以我要查找的dll的名称结尾。 在下面的代码中,CString路径确实包含我要查找的dll,但我不知道正则表达式失败的原因。使用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 );
#包括
const CString ValuesDLLName=_T(“MyDll.dll”);
boost::tregex EndsWithRegex(_T(“.+MyDll.dll\s*$”);
//boost::tregex EndsWithRegex1(_T(“^.+Values\.dll\\s*$”);//不起作用
//boost::tregex EndsWithRegex2(_T(“^.+Values\.dll\s*$”);//不起作用
//boost::tregex EndsWithRegex3(_T(“^.+Values.dll\s*$”);//不起作用
//boost::tregex EndsWithRegex4(_T(“^.+Values.dll\\s*$”);//不起作用
//boost::tregex EndsWithRegex5(_T(“^.+Values\.dll\\s*$”),boost::regex::perl);//不起作用
//boost::tregex EndsWithRegex6(_T(“^.+Values\.dll\s*$”),boost::regex::perl);//不起作用
//boost::tregex EndsWithRegex7(_T(“^.+Values.dll\s*$”),boost::regex::perl);//不起作用
//boost::tregex EndsWithRegex8(_T(“^.+Values.dll\\s*$”),boost::regex::perl);//不起作用
CString路径;
t匹配什么;
_tsearchenv(ValuesDLLName,_T(“PATH”)、PATH.GetBufferSetLength(260));
释放缓冲区(260);
bool endsWithDllName=boost::regex_搜索(路径、内容、EndsWithRegex);

> p>你的反斜杠需要加倍,因为C++将作为第一个转义字符吞并。试一试

boost::tregex EndsWithRegex( _T("^.+Values\\.dll\\s*$") );
另外,我认为您使用的
ReleaseBuffer
不正确。参数应该是返回的字符串的实际大小,否则字符串的结尾可能包含垃圾。如果您可以依赖于以null结尾的字符串,那么您可以始终使用-1作为参数,或者不使用-1,因为它是默认值