Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::regex_replace gives me unexpected result
    primarykey
    data
    text
    <p>I'm using <code>std::regex_replace</code> in a C++ Windows project (Visual Studio 2010). The code looks like this:</p> <pre><code>std::string str("http://www.wikipedia.org/"); std::regex fromRegex("http://([^@:/]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::string fmt("https://$1wik$2.org/"); std::string result = std::regex_replace(str, fromRegex, fmt); </code></pre> <p>I would expect <code>result</code> to be <code>"https://www.wikipedia.org/"</code>, but I get <code>"https://www.wikipedia.wikipedia.org/"</code>.</p> <p>A quick check with <code>sed</code> gives me the expected result</p> <pre><code>$ cat &gt; test.txt http://www.wikipedia.org/ $ sed 's/http:\/\/([^@:\/]+\.)?wik(ipedia|imedia)\.org\//https:\/\/$1wik$2.org\//' test.txt http://www.wikipedia.org/ </code></pre> <p>I don't get where the difference comes from. I checked the flags that can be used with <code>std::regex_replace</code>, I didn't see one that would help in this case.</p> <p><strong>Update</strong></p> <p>These variants work fine:</p> <pre><code>std::regex fromRegex("http://([^@:/]+\\.)wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::regex fromRegex("http://((?:[^@:/]+\\.)?)wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::regex fromRegex("http://([a-z]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::regex fromRegex("http://([^a]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); </code></pre> <p>bu not these:</p> <pre><code>std::regex fromRegex("http://([^1-9]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::regex fromRegex("http://([^@]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); std::regex fromRegex("http://([^:]+\\.)?wik(ipedia|imedia)\\.org/", std::regex_constants::icase); </code></pre> <p>It makes no sense to me...</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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