views:

62

answers:

1

Hi All

I having an issue in using Boost regex with a MFC CString. The regex is very simple: it must check if the string ends with the name of a dll I am looking for. In the code below the CString Path DOES contain the dll I am looking for, but I don't know why the regex is failing. Uisng ReleaseBuffer increases the buffer size so the Length of Path is set to MAX_PATH. Do you know why is not correct? I did a lot of attempts but always failing.

#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 );
+1  A: 

Your backslashes need to be doubled up, because C++ will swallow up the first one as an escape character. Try

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

Also I think you're using ReleaseBuffer incorrectly. The parameter should be the actual size of the string that was returned, otherwise the end of the string might contain garbage. If you can depend on the string being null terminated, you can always use -1 for the parameter, or leave it out since it's the default.

Mark Ransom