title: c++ 11 regex error
body: <p>Just an example code from <code>C++ Primer 5th Edition: 17.3.3. Using the Regular Expression Library</code></p>
  
  <p>Main file <code>main.cpp</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "regexcase.h"
  using namespace std;
  
  int main() {
      using_regex();
      return 0;
  }
  </code></pre>
  
  <p>Header file <code>regexcase.h</code>:</p>
  
  <pre><code>#ifndef REGEXCASE_H_
  #define REGEXCASE_H_
  
  #include &lt;regex&gt;
  #include &lt;string&gt;
  
  void using_regex();
  std::string parseCode(std::regex_constants::error_type etype);
  
  #endif /* REGEXCASE_H_ */
  </code></pre>
  
  <p>Source file <code>regexcase.cpp</code>:</p>
  
  <pre><code>#include "regexcase.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  void using_regex() {
      // look for words that violate a well-known spelling rule of thumb, "i before e, except after c":
      // find the characters ei that follow a character other than c
      string pattern("[^c]ei");
      // we want the whole word in which our pattern appears
      pattern = "[a-zA-Z]*" + pattern + "[a-zA-Z]*";  //[a-zA-Z]*   [[:alpha:]]*
      try {
          regex r(pattern, regex_constants::extended);    // construct a regex to find pattern                // , regex_constants::extended
          smatch results;     // define an object to hold the results of a search
          // define a string that has text that does and doesn't match pattern
          string test_str = "receipt freind theif receive";
          // use r to find a match to pattern in test_str
          if (regex_search(test_str, results, r)) // if there is a match
              cout &lt;&lt; results.str() &lt;&lt; endl;      // print the matching word
          else
              cout &lt;&lt; "no match for " &lt;&lt; pattern &lt;&lt; endl;
      } catch (regex_error &amp;e) {
          cout &lt;&lt; "what: " &lt;&lt; e.what() &lt;&lt; "; code: " &lt;&lt; parseCode(e.code()) &lt;&lt; endl;
      }
  }
  
  string parseCode(regex_constants::error_type etype) {
      switch (etype) {
      case regex_constants::error_collate:
          return "error_collate: invalid collating element request";
      case regex_constants::error_ctype:
          return "error_ctype: invalid character class";
      case regex_constants::error_escape:
          return "error_escape: invalid escape character or trailing escape";
      case regex_constants::error_backref:
          return "error_backref: invalid back reference";
      case regex_constants::error_brack:
          return "error_brack: mismatched bracket([ or ])";
      case regex_constants::error_paren:
          return "error_paren: mismatched parentheses(( or ))";
      case regex_constants::error_brace:
          return "error_brace: mismatched brace({ or })";
      case regex_constants::error_badbrace:
          return "error_badbrace: invalid range inside a { }";
      case regex_constants::error_range:
          return "erro_range: invalid character range(e.g., [z-a])";
      case regex_constants::error_space:
          return "error_space: insufficient memory to handle this regular expression";
      case regex_constants::error_badrepeat:
          return "error_badrepeat: a repetition character (*, ?, +, or {) was not preceded by a valid regular expression";
      case regex_constants::error_complexity:
          return "error_complexity: the requested match is too complex";
      case regex_constants::error_stack:
          return "error_stack: insufficient memory to evaluate a match";
      default:
          return "";
      }
  }
  </code></pre>
  
  <p>The output of calling <code>using_regex();</code> is <code>what: regex_error; code: error_brack: mismatched bracket([ or ])</code></p>
  
  <p>It seems that the regex can't <strong>parse</strong> the bracket.</p>
  
  <p>Refer to Answers in <a href="https://stackoverflow.com/questions/8060025/is-this-c11-regex-error-me-or-the-compiler">this question</a>, I use <code>regex_constants::extended</code> to initialize the regex object, which then is <code>regex r(pattern, regex_constants::extended);</code></p>
  
  <p>Then the output is <code>no match for [[:alpha:]]*[^c]ei[[:alpha:]]*</code></p>
  
  <p>It seems that the regex can't <strong>match</strong> the pattern.</p>
  
  <p>Then I use <code>[a-zA-Z]*</code> to replace character class <code>[[:alpha:]]*</code> (with <code>regex_constants::extended</code> still set). The output still is <code>no match for [a-zA-Z]*[^c]ei[a-zA-Z]*</code></p>
  
  <p>Platform: <code>windows</code></p>
  
  <p>Tools used: <code>Eclipse for C/C++</code>; <code>MinGW (g++ --version: g++ 4.7.2)</code></p>
  
  <p><strong>EDIT</strong>:
  Thanks @sharth, add main file to complete the code.</p>