8

Coming from perl-like regular expressions, I expected the below code to match regex'es in all 8 cases. But it doesn't. What am I missing?

#include <iostream>
#include <regex>
#include <string>

using namespace std;

void check(const string& s, regex re) {
    cout << s << " : " << (regex_match(s, re) ? "Match" : "Nope") << endl;
}

int main() {
    regex re1 = regex("[A-F]+", regex::icase);
    check("aaa", re1);
    check("AAA", re1);
    check("fff", re1);
    check("FFF", re1);
    regex re2 = regex("[a-f]+", regex::icase);
    check("aaa", re2);
    check("AAA", re2);
    check("fff", re2);
    check("FFF", re2);
}

Running with gcc 5.2:

$ g++ -std=c++11 test.cc -o test && ./test
aaa : Match
AAA : Match
fff : Nope
FFF : Match
aaa : Match
AAA : Match
fff : Match
FFF : Nope
glexey
  • 811
  • 7
  • 25
  • 2
    This looks like a libstdc++ bug. I think they're only checking the first character in the specified range when checking the *other* case. See this [example](http://coliru.stacked-crooked.com/a/abe033587520eece), where I changed the range to `"C-Z"` and then `"ccc"` matches, but everything else fails. clang with libc++ behaves as you expect it to. – Praetorian Jun 10 '16 at 22:34
  • 1
    as a workaround we use boost::regex when compiling for gcc. – Richard Hodges Jun 10 '16 at 23:59
  • 1
    A similar issue is observed with std::regex. Use Boost if you need case insensitivity with character classes. – Wiktor Stribiżew Jun 11 '16 at 10:39
  • 2
    This is being discussed in [#71500](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71500). – md5i Jun 11 '16 at 14:49
  • Does c++ support mode modifier? You may try this workaround `(?i)[A-F]+` – Julio Jul 11 '18 at 18:25

1 Answers1

3

For future users, this was confirmed to be a bug, and it (and similar issues) are solved as of 8.1 according to this thread.

I suspect that the original author had a hand in bringing attention to this, but I figured maybe we could get this closed.