C++ regex | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

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?

17th Apr 2017, 10:35 PM
Ulisses Cruz
Ulisses Cruz - avatar
1 Answer
+ 6
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);
17th Apr 2017, 10:46 PM
Squidy
Squidy - avatar