繁体   English   中英

如何将 std::regex_match 与 CString 一起使用?

[英]How to use std::regex_match with CString?

当我尝试将 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 的一部分(与启动 Visual 的 ATL 分开的“ATL 服务器”的一部分)工作室 2008)。 所以我不会使用CAtlRegExp

正则表达式中还有另一个错误:点匹配任何字符,因此正则表达式也匹配“1234567890”和“12x34y5678”,这确实是无效日期。 点必须被转义。 http://www.cplusplus.com/reference/regex/ECMAScript/

使用: const std::regex pattern("^[0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9][0-9][0-9]$");

As the others said, convert the csTest CString into a C string, while CString is the MFC string class and C string is a C language conformant string:

if (std::regex_match(csTest.GetString(), pattern)) ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM