1

regex_search is not matching "ter" in "Computer Computer" when I'm using following code -

#include <iostream>
#include <regex>

using namespace std;

int main() {
    string str("Computer Computer");
    cout << regex_search(str, regex("ter"));
    return 0;
}

The above code gives me 0, whereas there obviously is "ter" in the subject. So what is going on here. I know PHP's regex matches the substring just fine, but whats wrong here?

I'm using C++11 GCC, is that a problem since I read somewhere it has a poor support for regex.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Divya Mamgai
  • 148
  • 10

1 Answers1

1

It reruns 1 type bool, it means pattern match found in the word .Run this program.

#include <stdio.h>
#include<iostream>
#include <regex>
using namespace std;
int main(void) {
    cout << regex_search(string("Computer Computer"), regex("ter"));//1
    cout << regex_search(string("Computer Computer"), regex("tesdsr"));//0
    return 0;
}
Venkata Naidu M
  • 351
  • 1
  • 6
  • Close but no cigar, still getting 00. :( – Divya Mamgai Apr 25 '16 at 06:25
  • @DivyaMamgai this program is correct, but if you have the gcc compiler with version 4.8 or earlier, then [its Standard Library does not provide a working `` implementation](http://stackoverflow.com/q/12530406/819272). Upgrade to gcc 4.9 or later. – TemplateRex Apr 25 '16 at 09:37
  • Yes, just installed Cygwin with GCC 5.3 and everything is working great. – Divya Mamgai Apr 25 '16 at 19:36