MJF
MJF

Reputation: 83

simple regex_match in VS 2012 not working

I'm using VS 2012 C++. The following simple regex_match doesn't ever find what I'm looking for. I've stripped it down to the following. What am I missing?

#include <string>
#include <regex>
using namespace std;

int _tmain( int argc, char *argv[] )
{
   int i = 0;
   auto matches = smatch();
   regex rx( "." );
   string haystack( "ABC." );

   if( regex_match( haystack, matches, rx ) )
      i++;
   if( regex_match( haystack, rx ) )
      i++;
   if( regex_match( haystack.begin(), haystack.end(), rx ) )
      i++;
}

regex_match always returns false.

Upvotes: 1

Views: 282

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626871

You should use regex_search.

Note that regex_match will only successfully match a regular expression to an entire character sequence, whereas std::regex_search will successfully match subsequences.

Thus, this code will find the matches:

if( regex_search( haystack, matches, rx ) )
   i++;
if( regex_search( haystack, rx ) )
   i++;
if( regex_search( haystack.begin(), haystack.end(), rx ) )
   i++;

Upvotes: 1

Related Questions