title: Range of UTF-8 Characters in C++11 Regex
body: <p>This question is an extension of <a href="https://stackoverflow.com/questions/11254232/do-c11-regular-expressions-work-with-utf-8-strings">Do C++11 regular expressions work with UTF-8 strings?</a></p>
  
  <pre><code>#include &lt;regex&gt;  
  if (std::regex_match ("中", std::regex("中") ))  // "\u4e2d" also works
    std::cout &lt;&lt; "matched\n";
  </code></pre>
  
  <p>The program is compiled on Mac Mountain Lion with <code>clang++</code> with the following options:</p>
  
  <pre><code>clang++ -std=c++0x -stdlib=libc++
  </code></pre>
  
  <p>The code above works. This is a standard range regex <code>"[一-龠々〆ヵヶ]"</code> for matching any Japanese Kanji or Chinese character. It works in Javascript and Ruby, but I can't seem to get ranges working in C++11, even with using a similar version <code>[\u4E00-\u9fa0]</code>. The code below does not match the string.</p>
  
  <pre><code>if (std::regex_match ("中", std::regex("[一-龠々〆ヵヶ]")))
    std::cout &lt;&lt; "range matched\n";
  </code></pre>
  
  <p>Changing locale hasn't helped either. Any ideas?</p>
  
  <h1>EDIT</h1>
  
  <p>So I have found that all ranges work if you add a <code>+</code> to the end. In this case <code>[一-龠々〆ヵヶ]+</code>, but if you add <code>{1}</code> <code>[一-龠々〆ヵヶ]{1}</code> it does not work. Moreover, it seems to overreach it's boundaries. It won't match latin characters, but it will match <code>は</code> which is <code>\u306f</code> and <code>ぁ</code> which is <code>\u3041</code>. They both lie below <code>\u4E00</code></p>
  
  <p>nhahtdh also suggested regex_search which also works without adding <code>+</code> but it still runs into the same problem as above by pulling values outside of its range. Played with the locales a bit as well. Mark Ransom suggests it treats the UTF-8 string as a dumb set of bytes, I think this is possibly what it is doing.</p>
  
  <p>Further pushing the theory that UTF-8 is getting jumbled some how, <code>[a-z]{1}</code> and <code>[a-z]+</code> matches <code>a</code>, but only <code>[一-龠々〆ヵヶ]+</code> matches any of the characters, not <code>[一-龠々〆ヵヶ]{1}</code>.</p>