0
为什么在下面的输出这段代码的结果可以请人向我解释:C++ 11 regex_replace怪异的行为
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main(void) {
std::string test_string("foo.bar.baz.<name>:<value>|@<rate>|#tag:<tag>");
if (std::regex_match(test_string, std::regex(".*(<name>).*"))) {
std::cout << "MATCH!" << std::endl;
} else {
std::cout << "NO MATCH!" << std::endl;
}
test_string = std::regex_replace(test_string, std::regex(".*(<name>).*"), "master");
std::cout << test_string << std::endl;
return 0;
}
输出:
[[email protected] tmp]# ./test
MATCH!
master
这在我看来,运行时被打破。在下面的页面搜索正则表达式,没有实现。
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011
does not work。它杀死了整个字符串:[root @ 0be8f48dd8d3 /]#./test 匹配! 主人 – ladaManiak
那么,你告诉它用“主人”替换“东西,然后是,跟着别的东西”。如果您只想替换“”,请仅将其用作正则表达式。 C++没有“只替换这个子匹配”功能。 –
好点。我很困惑,regex_replace使用匹配来标识要替换的字符串。现在有道理。谢谢! – ladaManiak