Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 regex::icase inconsistent behavior

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
like image 413
glexey Avatar asked Jun 10 '16 22:06

glexey


People also ask

Why is std :: regex slow?

The current std::regex design and implementation are slow, mostly because the RE pattern is parsed and compiled at runtime. Users often don't need a runtime RE parser engine as the pattern is known during compilation in many common use cases.

What is regex error?

Defined in header <regex> class regex_error; (since C++11) Defines the type of exception object thrown to report errors in the regular expressions library.

What does regex mean in C++?

Regular Expression (regex) In C++ A regular expression or regex is an expression containing a sequence of characters that define a particular search pattern that can be used in string searching algorithms, find or find/replace algorithms, etc. Regexes are also used for input validation.

Does C++ have regex?

std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++ Regex is the short form for “Regular expression”, which is often used in this way in programming languages and many different libraries. It is supported in C++11 onward compilers.


1 Answers

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.

like image 172
Harrison Mc Avatar answered Sep 21 '22 02:09

Harrison Mc