Regular Expression in C++/ Star Problem
I have no idea why the regular expression does not match the string below:
int main(){
string seq = "0010110";
regex rgx("((1*(01)*1*)*)(00)(1*(01)*1*)*(10)");
cout<&l开发者_如何学运维t;regex_match(seq, rgx)<<endl;
system("pause");
return 0;
}
The problem is resolved when I remove the last star, which multiplies a big string.
Please help me.
This may be a quirk of your library (or usage) in treating the regex greedily. (00)
gets 00 (1*(01)*1*)*
sucks up 1011
and then the remaining (10)
doesn't match the one last 0. Then for some reason your library isn't deciding to backtrack and try another match (thanks @Paul Rubel, @marcog).
精彩评论