Michael Potanin
Michael Potanin

Reputation: 51

what is wrong with regex_match? very simple expression

I'm using VS2010 and coding c++ console application and faced the problem

#include <regex>
using namespace std;

//...

if (!regex_match("abab",regex("(ab?)*")))
{
  //the problem is - why we are here? why it doesn't match?
}

checked here http://regexpal.com/ - it matches

Upvotes: 5

Views: 218

Answers (1)

St0fF
St0fF

Reputation: 1633

Very simple: regex_match only returns true if the entire sequence gets matched. You may want to use regex_search if you want to see if a string contains your regex.

"ab?" matches "aba", the repeater ("()*")make this match once. The remainder is "b", so it's not a full match.

I'm sorry, I misread the regex. It should be a full match. Weird enough:

regex_match("aab", regex("(ab?)*")) == true

Seems to be a bug within the stl used (tested with QT Creator 2010.05, makespec = VS2010). Replacing regex_match with regex_search in your code matches right, but the match_results are empty - indicating something still goes wrong.

With VS2012 all tests matched correctly.

Upvotes: 1

Related Questions