Each time that I try to initialize a regular expression by a pattern which contains a class-expression (something surrounded by [
and ]
) my program ends throwing a regex_error
.
For definiteness, this could be the program:
// main.cpp
#include <regex>
int main(){
std::regex r("[ab]*");
}
I managed to build the program successfully using either
g++ -std=c++11 main.cpp -o main.exe
(I have gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)
)
or
clang++ -std=c++11 main.cpp -o main.exe
(with Debian clang version 3.2-7ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
)
However, in both cases executing main.exe
gives me:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
The curiousity is that the whole thing does work for patterns which do not include class-brackets ([
and ]
) as, e.g., std::regex r("ab*")
.
What could be the problem?
Thanks already!
Update:
Problem worked around in two ways with help of the comments of (1) ecatmur and (2) Cubbi.
(1) Either, you can install boost and use boost::regex
(after making sure that the boost root directory is in your include path, then using #include <boost/regex.hpp>
) and linking the boost_regex
library (for me this means compiling like this: g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex main.cpp -o main.exe
).
(2) Or, you can install libc++ and use clang++ -std=c++11 -stdlib=libc++ main.cpp -o main.exe
Thx for the help!