Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is wrong with regex_match? very simple expression

Tags:

c++

regex

std

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

like image 457
Michael Potanin Avatar asked Nov 10 '22 02:11

Michael Potanin


1 Answers

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.

like image 111
St0fF Avatar answered Nov 14 '22 21:11

St0fF