What is wrong with regex_match? very simple expression

I am using VS2010 and C ++ coding console application and have encountered a problem

#include <regex> using namespace std; //... if (!regex_match("abab",regex("(ab?)*"))) { //the problem is - why we are here? why it doesn't match? } 

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

+5
source share
1 answer

Very simple: regex_match returns true only if the entire sequence matches. You can use regex_search if you want to see if the string contains your regular expression.

"anyhow?" matches "aba", the repeater ("() *") makes this match once. The rest of the value is "b", so it does not match completely.

Sorry, I am not reading the regular expression correctly. It must be a complete match. Strange enough:

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

This seems to be a bug in the stl used (tested with QT Creator 2010.05, makepec = VS2010). Replacing regex_match with regex_search in your code matches the rule, but match_results are empty, which indicates that something went wrong.

With VS2012, all tests match correctly.

0
source

Source: https://habr.com/ru/post/1201577/


All Articles