2

I am having trouble extracting the token values from my string : "JOIN #ROOM\r\n" I am compiling my code on Mingw64 with the following arguments : g++ tregex.cpp -o tregex.exe -std=gnu++11

I get this error , but not my exception for some reason :

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. terminate called after throwing an instance of 'std::regex_error' what(): regex_error

This is my code :

#include <regex>
#include <string>
#include <iostream>
using namespace std;

//Tregex.cpp

int main(void) {
    regex rgx("[[:cntrl:]]");
    string str = "JOIN  #ROOM\r\n";
    smatch match;
    try{
        if(regex_search(str, match, rgx))
            for(auto token:match) cout << token <<"\n";
        cout<< endl;
    }
    catch(regex_error & e){
        if( e.code() == regex_constants::error_escape )
            cerr << "invalid escape character \n";
        else if( e.code() == regex_constants::error_stack )
            cerr << "regular expression is not big enough\n";
        else
            cerr << "exception caught: "<< e.what()<<"\n";
    }
    cin.get();
    return 0;
}
HeroofCode
  • 21
  • 1

1 Answers1

0

FWIW, there's nothing wrong with your C++ code, and it works perfectly using Clang and libc++ on my MacBook.

As indicated by the comments above, <regex> is one of the features that works in the LLVM project's libc++ but has never worked properly in the GNU project's libstdc++. You might try switching to libc++ if it's available for your platform.

Community
  • 1
  • 1
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159