2

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

Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
  • not yet fully supported, considering . it worked, see this [post](http://stackoverflow.com/questions/8060025/is-this-c11-regex-error-me-or-the-compiler). – gongzhitaao May 03 '13 at 01:37
  • possible duplicate of [No matches with c++11 regex](http://stackoverflow.com/questions/11269766/no-matches-with-c11-regex) – nhahtdh May 03 '13 at 04:45
  • @gongzhitaao - "not yet fully supported" doesn't do it justice. There's nothing useful there, and the useless `` header should not have been put into gcc's library implementation. – Pete Becker May 03 '13 at 11:50

2 Answers2

1

If you are compiling with g++ it may be because regex is not fully supported yet. See here for current C++11 status in g++.

Dr.Tower
  • 985
  • 5
  • 11
  • I added my compiler flag sup above, I guess I'm going to try to switch to visual studio instead of emacs with g++ – Tyler Pfaff May 03 '13 at 01:46
  • 1
    @TylerPfaff No need to get drastic. You could just use boost/regex or one of the other available regex implementations out there. – Cubic May 03 '13 at 08:11
1

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

aaronman
  • 18,343
  • 7
  • 63
  • 78