Regex confusion, regex_search() returning false when it seems like it should return true

Nov 27, 2015 at 3:29am
Apologizes if this is blindingly easy or already in the forums, but I decided to ask about it because I have been searching/googling all day without any success. I am trying to get a regex expression to pass, a very, very simple test, without luck.
Here is an example of what I am trying to do:
1
2
3
4
5
6
    std::regex letters("[a-zA-Z]",std::regex_constants::basic);
    if(std::regex_search("WHATEVER",letters)){
        std::cout << "Passed" << std::endl; 
    }else {
        std::cout << "Failed" << std::endl; 
    }

I have tried a bunch of different variations of the pattern, I have been trying different variations all day, anything and everything that google turned up, but what I am trying to do is very simple, just check if there exists in a string, at least one letter. regex_search() returns false for every pattern I have tried. I feel like I am missing something fundamental here, so any insight would be amazing. Thank you!
Nov 27, 2015 at 4:44am
You wouldn't happen to be using a version of gcc where regex isn't implemented, would you?
Nov 27, 2015 at 2:49pm
No, I don't think that is it. It compiles just fine, with the -std=c++11 flag. I am using version 4.8.4, came with Ubuntu. It compiles fine and runs without error, but returns false. So the above program prints "Failed".
Nov 27, 2015 at 3:04pm
gcc didn't implement regex until version 5.04.9. Earlier versions had non-functioning stubs for some of regex functions, which compiled (quite misleadingly)

Modern gcc compiles your example as you would expect: http://coliru.stacked-crooked.com/a/de0db7dca12885c5

(other compilers, such as clang and visual studio, had the C++11 regex working since 2010)
Last edited on Nov 27, 2015 at 3:23pm
Nov 27, 2015 at 3:05pm
I am using version 4.8.4

You should update to 4.9 or later.

It compiles fine and runs without error, but returns false.

Yes. It does that.

See:
http://stackoverflow.com/a/12665408
Last edited on Nov 27, 2015 at 3:08pm
Nov 27, 2015 at 3:39pm
Oh wow, ok. thanks!!! I just totally assumed that if it was compiling and running without throwing a runtime error that it supported regex! Thanks for pointing me in the right direction.
Topic archived. No new replies allowed.