4

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!

Community
  • 1
  • 1
Leolo
  • 416
  • 1
  • 5
  • 12
  • Apparently, `` will work in gcc 4.9; if you can't wait till that's released, maybe use boost? – ecatmur Feb 04 '14 at 20:11
  • I have seen the suggested post... That's why I tried clang. I read somewhere it works fine. Actually , in the very same post [tunnuz](http://stackoverflow.com/users/25418/tunnuz) mentions that his code works with LLVM - though there are no brackets in his pattern. – Leolo Feb 04 '14 at 20:16
  • IIRC LLVM on Debian uses libstdc++ so you're using the same regex implementation, though. – ecatmur Feb 04 '14 at 20:19
  • Does this mean that there is no working std::regex implementation available for my system (linux / ubuntu) right now? – Leolo Feb 04 '14 at 20:26
  • Depends how much work you're willing to go to; the gcc 4.9 implementation is in SVN trunk so you could use that. Otherwise just use boost.regex (http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/index.html) – ecatmur Feb 04 '14 at 20:27
  • You can also install LLVM libc++ on Linux if you'd like, from http://libcxx.llvm.org/ - your example would work then: http://coliru.stacked-crooked.com/a/bfbc3755ba96f244 , but boost is the safer bet. – Cubbi Feb 04 '14 at 20:38
  • Thanks a lot! I have tried both solutions -- both work for all examples I tested. I'll add them to my original post. – Leolo Feb 04 '14 at 21:43

0 Answers0