I have some code that uses the CAtlRegExp to perform some validation. In the particular case, a zip-code (5 or 5-4 format). In addition, I want the data to be exactly that, so I’m using anchors (^ and $). When I use the anchor-less expression the Match call succeeds. When I add the anchors, the Match call fails with the exact same data.
The code below is what I’m doing, with the values supplied in code for the failing case. I’ll admit to not being a regular expression wizard, but RegEx Buddy is happy w/ the format expression as it is in code.
Any help would be appreciated. And if I’m missing anything, please let me know.
Thanks.
sData = "12345-1234";
m_FormatExpression = "^(\\d{5})(-(\\d{4}))?$";
CAtlRegExp<> regex;
REParseError status = regex.Parse( m_FormatExpression );
if( REPARSE_ERROR_OK == status )
{
CAtlREMatchContext<> match;
if( regex.Match( sData, &match ) )
{
result = true;
}
}
Edit: Completely changed my answer when I figured out the problem.
The CAtlRegExp class does not support the repeat count in curly braces
{}; it uses those for returning match objects. The5and4in your expression were literal characters that it had to match. It’s just a coincidence that the string you were testing with had a5in it, while the part with the4was optional. This expression works perfectly: