Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression in C++/ Star Problem

Tags:

c++

regex

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<<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.

like image 916
Billy Avatar asked Mar 24 '26 20:03

Billy


1 Answers

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).

like image 101
Mark B Avatar answered Mar 27 '26 09:03

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!