Conditionally replace regex matches in string

前端 未结 1 972
温柔的废话
温柔的废话 2020-12-03 17:28

I am trying to replace certain patterns in a string with different replacement patters.

Example:

string test = \"test replacing \\\"these characters\         


        
相关标签:
1条回答
  • 2020-12-03 17:53

    The c++ (0x, 11, tr1) regular expressions do not really work (stackoverflow) in every case (look up the phrase regex on this page for gcc), so it is better to use boost for a while.

    You may try if your compiler supports the regular expressions needed:

    #include <string>
    #include <iostream>
    #include <regex>
    
    using namespace std;
    
    int main(int argc, char * argv[]) {
        string test = "test replacing \"these characters\"";
        regex reg("[^\\w]+");
        test = regex_replace(test, reg, "_");
        cout << test << endl;
    }
    

    The above works in Visual Studio 2012Rc.

    Edit 1: To replace by two different strings 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 (/e switch).

    Therefore, you'll need two passes, as you already suspected:

     ...
     string test = "test replacing \"these characters\"";
     test = regex_replace(test, regex("\\s+"), "_");
     test = regex_replace(test, regex("\\W+"), "");
     ...
    

    Edit 2:

    If it would be possible to use a callback function tr() in regex_replace, then you could modify the substitution there, like:

     string output = regex_replace(test, regex("\\s+|\\W+"), tr);
    

    with tr() doing the replacement work:

     string tr(const smatch &m) { return m[0].str()[0] == ' ' ? "_" : ""; }
    

    the problem would have been solved. Unfortunately, there's no such overload in some C++11 regex implementations, but Boost has one. The following would work with boost and use one pass:

    ...
    #include <boost/regex.hpp>
    using namespace boost;
    ...
    string tr(const smatch &m) { return m[0].str()[0] == ' ' ? "_" : ""; }
    ...
    
    string test = "test replacing \"these characters\"";
    test = regex_replace(test, regex("\\s+|\\W+"), tr);   // <= works in Boost
    ...
    

    Maybe some day this will work with C++11 or whatever number comes next.

    Regards

    rbo

    0 讨论(0)
提交回复
热议问题