Using regex classes in C ++

Every time I try to initialize a regular expression with a template containing a class expression (something surrounded by [and ]), my program finishes throwing regex_error.
For definiteness, this may be a program:

// main.cpp
#include <regex>
int main(){
    std::regex r("[ab]*"); 
}

I managed to create the program successfully using

g++ -std=c++11 main.cpp -o main.exe

(Mine gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9))
or

clang++ -std=c++11 main.cpp -o main.exe

(c Debian clang version 3.2-7ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2))
However, in both cases, execution main.exegives me:

terminate called after throwing an instance of 'std::regex_error'
what():  regex_error

Curiously, all this works for patterns that do not include cluster brackets ( [and ]) such as, for example std::regex r("ab*").
What could be the problem? Thanks already!


:
(1) ecatmur (2) Cubbi.
(1) boost boost::regex (, boost , #include <boost/regex.hpp>) boost_regex ( : g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex main.cpp -o main.exe).
(2) lib++ clang++ -std=c++11 -stdlib=libc++ main.cpp -o main.exe
!

+4

Source: https://habr.com/ru/post/1525303/


All Articles