C++ regex | SoloLearn: Learn to code for FREE!


C++ regex

I'm trying to use regular expressions in c++. Example code: ///////////////////////////////////////////////////////// #include <regex> int main(){ std::regex pattern("([a-z]+)\\.txt"); return 0; } ///////////////////////////////////////////////////////// The code will compile. But when I run it I get regex_error exception. I'm using this to compile it: > g++ -std=c++11 example.cpp my compiler version is 4.8.4. Can someone help me?

4/17/2017 10:35:46 PM

Ulisses Cruz

1 Answer

New Answer


ECMAScript is the default syntax for the standard library's regular expressions, which doesn't support brackets. For your regex to work, use the Basic POSIX flag: std::regex pattern("([a-z]+)\\.txt", std::regex_constants::basic);