When I try to use std::regex with CString (MFC) I get this error:

And this is the code:
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))
There's no unambiguous conversion of CString to the first parameter of any of std::regex_match overloads.
Add GetString() to convert to const TCHAR* explicitly:
std::regex_match(csTest.GetString(), pattern)
Or, if you want to make use of iterator range (which could be a micro-optimization) use GetLength() in addition:
std::regex_match(csTest.GetString(), csTest.GetString() + csTest.GetLength(), pattern)
A comment recommends using CAtlRegExp. You can do this too, but note that CAtlRegExp has non-standard syntax, and also it has some bugs, and even no longer a part of ATL that ships with Visual Studio (part of "ATL server" that is separate from ATL starting Visual Studio 2008). So I would not use CAtlRegExp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With