title: Using regex for input validation
body: <p>I know the following isn't the most efficient implementation (I could, after all, run regex_match() right in main() ) but it should still work, at least from my limited understanding.</p>
  
  <p>I'm trying to use  in C++11 to validate some input for dicerolls. I used expresso to build the regular expressions, and it validates it within expresso just fine. Why would it fail within this code?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;regex&gt;
  
  bool regexValidate (string expr, string teststring) 
  {
      regex ex(expr);
      if (regex_match(teststring,ex)) {
          return true;
          cout &lt;&lt; "true";
      } else if (regex_match (teststring,regex("^\s*\d*d\d*\s*$"))) {
          return true;
          cout &lt;&lt; "true";
      }
      return false;
  }
  
  int main() 
  {
      char    choice;             // menu option selected by user
      bool    isExit = false;     // Is the user ready to exit?         
  
      // test code
      string  diceexpr = "^\s*\d{1,}d\d{1,}\s*$";
      string  teststr = "10d10";
      string  teststr1 = "1d10";
      string  teststr2 = " 10d10 ";
  
      cout &lt;&lt; teststr &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      cout &lt;&lt; teststr1 &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr1)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      cout &lt;&lt; teststr2 &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr2)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>Sample output:</p>
  
  <pre><code>10d10 is  invalid!
  1d10 is  invalid!
   10d10  is  invalid!
  
  RUN SUCCESSFUL (total time: 291ms)
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Corrected my code and changed it to use the boost library for regular expressions.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/regex.hpp&gt;
  using namespace std;
  
  bool regexValidate (string expr, string teststring) 
  {
      boost::regex ex(expr);
      if ( boost::regex_match (teststring,ex) ) {
          cout &lt;&lt; "true";
          return true;
      //} else if (regex_match (teststring,regex("^\s*\d*d\d*\s*$"))) {
      //    cout &lt;&lt; "true";
      //    return true;
      }
      return false;
  }
  
  int main()
  {
      string  diceexpr = "^\\s*\\d{1,}d\\d{1,}\\s*$";
      string  teststr = "10d10";
      string  teststr1 = "1d10";
      string  teststr2 = " 10d10 ";
  
      cout &lt;&lt; teststr &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      cout &lt;&lt; teststr1 &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr1)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      cout &lt;&lt; teststr2 &lt;&lt; " is ";
      if (regexValidate(diceexpr,teststr2)) {
          cout &lt;&lt; " valid!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; " invalid!" &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>Given the following output:</p>
  
  <pre><code>terminate called after throwing an instance of 'std::regex_error'
    what():  regex_error
  
  RUN FAILED (exit value 1, total time: 1s)
  </code></pre>