MFC CString 导致 boost 正则表达式搜索失败
我在将 Boost 正则表达式与 MFC CString 一起使用时遇到问题。 正则表达式非常简单:它必须检查字符串是否以我正在查找的 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 );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的反斜杠需要加倍,因为 C++ 会吞掉第一个反斜杠作为转义字符。 另请尝试
我认为您使用
ReleaseBuffer
的方式不正确。参数应该是返回的字符串的实际大小,否则字符串末尾可能包含垃圾。如果您可以依赖以 null 结尾的字符串,则始终可以使用 -1 作为参数,或者将其保留,因为它是默认值。Your backslashes need to be doubled up, because C++ will swallow up the first one as an escape character. Try
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.