Reputation: 5052
I'm trying to use a regular expression to accept strings that have sequences like
ifelseifelseififif
So every else needs an if, but not every if needs an else. I know I could do this with pen and paper with a simple regular expression like this ((if)*+(ifelse)*)*
.Now I'm not sure if I'll be able to do this with the library as I've never used it before. So would it be possible to accept or reject a string based on a regular expression like the one I wrote above?
I wrote this sample to get my feet wet and I don't understand why it returns false. Isn't regex_search() supposed to find substring matches? That snippet prints nope every time.
#include <regex>
#include <iostream>
using namespace std;
int main(){
string sequence="ifelse";
regex rx("if");
if(regex_search(sequence.begin(),sequence.end(),rx)){
cout<<"match found"<<endl;
}else{
cout<<"nope"<<endl;
}
}
I'm using g++ 4.7 and have tried compiling with both g++ -std=gnu+11 reg.cpp
and g++ -std=c++11 reg.cpp
Upvotes: 2
Views: 3499
Reputation: 18771
This prints "match found", I just ran it. It wouldn't compile if you weren't using c++11 but heres how I compiled it.
clang++ -std=c++11 -stdlib=libc++ reg.cpp
Upvotes: 1