Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The c++ (0x, 11, tr1) regular expressions <a href="https://stackoverflow.com/q/4716680/170194">do not really work (stackoverflow)</a> in every case (look up the <a href="http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x" rel="nofollow noreferrer">phrase <strong>regex</strong> on <strong>this page</strong></a> for gcc), so it is better to <a href="http://www.boost.org/libs/regex/" rel="nofollow noreferrer">use boost</a> for a while. </p> <p>You may try if your compiler supports the regular expressions needed:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;regex&gt; using namespace std; int main(int argc, char * argv[]) { string test = "test replacing \"these characters\""; regex reg("[^\\w]+"); test = regex_replace(test, reg, "_"); cout &lt;&lt; test &lt;&lt; endl; } </code></pre> <p>The above works in Visual Studio 2012Rc.</p> <p><strong>Edit 1</strong>: To replace by <strong>two different strings</strong> in one pass (depending on the match), I'd think this won't work here. In Perl, this could easily be done within evaluated replacement expressions (<code>/e</code> switch).</p> <p>Therefore, you'll need two passes, as you already suspected:</p> <pre><code> ... string test = "test replacing \"these characters\""; test = regex_replace(test, regex("\\s+"), "_"); test = regex_replace(test, regex("\\W+"), ""); ... </code></pre> <p><strong>Edit 2</strong>:</p> <p>If it would be possible to use a <strong>callback function</strong> <code>tr()</code> in <code>regex_replace</code>, then you could modify the substitution there, like:</p> <pre><code> string output = regex_replace(test, regex("\\s+|\\W+"), tr); </code></pre> <p>with <code>tr()</code> doing the replacement work:</p> <pre><code> string tr(const smatch &amp;m) { return m[0].str()[0] == ' ' ? "_" : ""; } </code></pre> <p>the problem would have been solved. Unfortunately, there's <strong>no such overload</strong> in some C++11 regex implementations, but Boost <strong><a href="http://boost.2283326.n4.nabble.com/regex-Context-sensitive-replacement-of-text-using-regex-replace-tp2589269p2589275.html" rel="nofollow noreferrer">has one</a></strong>. The following would work with boost and use one pass:</p> <pre><code>... #include &lt;boost/regex.hpp&gt; using namespace boost; ... string tr(const smatch &amp;m) { return m[0].str()[0] == ' ' ? "_" : ""; } ... string test = "test replacing \"these characters\""; test = regex_replace(test, regex("\\s+|\\W+"), tr); // &lt;= works in Boost ... </code></pre> <p>Maybe some day this will work with C++<strong>11</strong> or whatever number comes next.</p> <p>Regards</p> <p>rbo</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload