且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有条件地替换字符串中的正则表达式匹配

更新时间:2022-12-17 12:50:06

在每种情况下,c ++(0x,11,tr1)正则表达式都不会真正工作(***)此页面查找短语正则表达式 for gcc),因此***使用boost 一段时间。



您可以尝试如果您的编译器支持所需的正则表达式:

  #include< string> 
#include< iostream>
#include< regex>

using namespace std;

int main(int argc,char * argv []){
string test =test replaced \these characters\;
regex reg([^ \\w] +);
test = regex_replace(test,reg,_);
cout<<测试< endl;
}

上述工作原理在Visual Studio 2012Rc。



编辑1 :要在一次传递中替换两个不同的字符串(取决于匹配项),我认为这不会在这里工作。在Perl中,这可以在已评估的替换表达式( / e switch)中轻松完成。



您需要两次通过,如您已经怀疑:

  ... 
string test =test replaced \这些字符\;
test = regex_replace(test,regex(\\ss),_);
test = regex_replace(test,regex(\\W +),);
...

编辑2



如果可以在回调函数 tr() > regex_replace ,那么你可以修改替换,例如:

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

tr()

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

问题会被解决。不幸的是,在一些C ++ 11正则表达式实现中有没有这样的重载,但Boost 有一个 。以下将使用boost并使用一个pass:

  ... 
#include< boost / regex。 hpp>
using namespace boost;
...
string tr(const smatch& m){return m [0] .str()[0] ==''? _:; }
...

string test =测试替换\这些字符\;
test = regex_replace(test,regex(\\s + | \\W +),tr); //< = works in Boost
...

也许有一天,这将工作



rbo

p>

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

Example:

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

What I want to do is replace all ' ' with '_' and all other non letter or number characters with an empty string. I have the following regex created and it seems to tokenize correctly, but I am not sure how to (if possible) perform a conditional replace using regex_replace.

string test = "test replacing \"these characters\"";
regex reg("(\\s+)|(\\W+)");

expected result after replace would be:

string result = "test_replacing_these_characters";

EDIT: I cannot use boost, which is why I left it out of the tags. So please no answer that includes boost. I have to do this with the standard library. It may be that a different regex would accomplish the goal or that I am just stuck doing two passes.

EDIT2: I did not remember what characters were included in \w at the time of my original regex, after looking it up I have further simplified the expression. Again the goal is anything matching \s+ should be replaced with '_' and anything matching \W+ should be replaced with empty string.

The c++ (0x, 11, tr1) regular expressions do not really work (***) 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