当我尝试将 std::regex 与 CString (MFC) 一起使用时,出现此错误:
这是代码:
const std::regex pattern("^[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]$");
const CString& csTest = "28.10.1991";
if (std::regex_match(csTest, pattern))
最佳答案
没有将 CString
明确转换为任何 std::regex_match
重载的第一个参数。
添加 GetString()
以显式转换为 const TCHAR*:
std::regex_match(csTest.GetString(), pattern)
或者,如果您想使用迭代器范围(这可能是微优化),请另外使用 GetLength()
:
std::regex_match(csTest.GetString(), csTest.GetString() + csTest.GetLength(), pattern)
一条评论建议使用CAtlRegExp
。您也可以这样做,但请注意 CAtlRegExp
具有非标准语法,并且它还有一些错误,甚至不再是 Visual Studio 附带的 ATL 的一部分(“ATL 服务器”的一部分)从 Visual Studio 2008 开始,它与 ATL 是分开的)。所以我不会使用 CAtlRegExp
。
关于c++ - 如何将 std::regex_match 与 CString 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352173/