Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use std::regex_match with CString?

Tags:

c++

regex

mfc

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

enter image description here

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))
like image 456
harunB10 Avatar asked May 07 '26 00:05

harunB10


1 Answers

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.

like image 77
Alex Guteniev Avatar answered May 08 '26 15:05

Alex Guteniev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!