1

I'm trying regex on MinGW (see code sample) but I'm getting

"what: regex_error; code: error_escape: invalid escape character or trailing escape"

message that means that error_escape exception is thrown. I don't understand why because code works in Visual Studio 2013 (Windows 7) and in Mac OSX environment.

Compilation and linking command in Sublime Text is:

"cmd": ["g++", "-std=c++11", "$file_name", "-o", "${file_base_name}.exe", "-lm", "-Wall", "&&", "${file_base_name}.exe"] 

#include <iostream>
#include <string>
#include <regex>
#include <iterator>

std::string parseCode(std::regex_constants::error_type etype) {
    switch (etype) {
    case std::regex_constants::error_collate:
        return "error_collate: invalid collating element request";
    case std::regex_constants::error_ctype:
        return "error_ctype: invalid character class";
    case std::regex_constants::error_escape:
        return "error_escape: invalid escape character or trailing escape";
    case std::regex_constants::error_backref:
        return "error_backref: invalid back reference";
    case std::regex_constants::error_brack:
        return "error_brack: mismatched bracket([ or ])";
    case std::regex_constants::error_paren:
        return "error_paren: mismatched parentheses(( or ))";
    case std::regex_constants::error_brace:
        return "error_brace: mismatched brace({ or })";
    case std::regex_constants::error_badbrace:
        return "error_badbrace: invalid range inside a { }";
    case std::regex_constants::error_range:
        return "erro_range: invalid character range(e.g., [z-a])";
    case std::regex_constants::error_space:
        return "error_space: insufficient memory to handle this regular expression";
    case std::regex_constants::error_badrepeat:
        return "error_badrepeat: a repetition character (*, ?, +, or {) was not preceded by a valid regular expression";
    case std::regex_constants::error_complexity:
        return "error_complexity: the requested match is too complex";
    case std::regex_constants::error_stack:
        return "error_stack: insufficient memory to evaluate a match";
    default:
        return "";
    }
  }

void regexMatchExample()
{
  if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
    std::cout << "string literal matched\n";

  const char cstr[] = "subject";
  std::string s ("subject");
  std::regex e ("(sub)(.*)");

  if (std::regex_match (s,e))
    std::cout << "string object matched\n";

  if ( std::regex_match ( s.begin(), s.end(), e ) )
    std::cout << "range matched\n";

  std::cmatch cm;    // same as std::match_results<const char*> cm;
  std::regex_match (cstr,cm,e);
  std::cout << "string literal with " << cm.size() << " matches\n";

  std::smatch sm;    // same as std::match_results<string::const_iterator> sm;
  std::regex_match (s,sm,e);
  std::cout << "string object with " << sm.size() << " matches\n";

  std::regex_match ( s.cbegin(), s.cend(), sm, e);
  std::cout << "range with " << sm.size() << " matches\n";

  // using explicit flags:
  std::regex_match ( cstr, cm, e, std::regex_constants::match_default );

  std::cout << "the matches were: ";
  for (unsigned i=0; i<sm.size(); ++i) {
    std::cout << "[" << sm[i] << "] ";
  }

  std::cout << std::endl;
}




void regexSearchExample()
{
  std::string s ("this subject has a submarine as a subsequence");

  std::smatch m;
  try {

    std::regex e("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

    std::cout << "Target sequence: " << s << std::endl;
    std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
    std::cout << "The following matches and submatches were found:" << std::endl;

    while (std::regex_search(s,m,e)) {
      for (auto x:m) std::cout << x << " ";
      std::cout << std::endl;
      s = m.suffix().str();
    }

  } catch (std::regex_error &e) {
    std::cout << "what: " << e.what() << "; code: " << parseCode(e.code()) << std::endl;
  }

}



int main ()
{
  std::cout << "######## regexMatchExample ############" << std::endl;
  regexMatchExample();
  std::cout << "######## regexSearchExample ############" << std::endl;
  regexSearchExample();

  return 0;
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Guntis
  • 31
  • 2

0 Answers0