MFC CString 导致 boost 正则表达式搜索失败

发布于 2024-09-19 18:51:15 字数 1267 浏览 3 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-09-26 18:51:15

你的反斜杠需要加倍,因为 C++ 会吞掉第一个反斜杠作为转义字符。 另请尝试

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文