After asking this question on SO, I realised that I needed to replace all matches within a string with another string. In my case, I want to replace all occurrences of a whitespace with `\s*' (ie. any number of whitespaces will match).
在SO上询问这个问题之后,我意识到我需要用另一个字符串替换字符串中的所有匹配项。在我的情况下,我想用'\ s *'替换所有出现的空格(即任意数量的空格将匹配)。
So I devised the following:
所以我设计了以下内容:
#include <string>
#include <regex>
int main ()
{
const std::string someString = "here is some text";
const std::string output = std::regex_replace(someString.c_str(), std::regex("\\s+"), "\\s*");
}
This fails with the following output:
这失败,输出如下:
error: no matching function for call to ‘regex_replace(const char*, std::regex, const char [4])
错误:没有用于调用'regex_replace的匹配函数(const char *,std :: regex,const char [4])
Working example: http://ideone.com/yEpgXy
工作示例:http://ideone.com/yEpgXy
Not to be discouraged, I headed over to cplusplus.com and found that my attempt actually matches the first prototype of the regex_replace
function quite well, so I was surprised the compiler couldn't run it (for your reference: http://www.cplusplus.com/reference/regex/match_replace/)
不要气馁,我前往cplusplus.com,发现我的尝试实际上与regex_replace函数的第一个原型完全匹配,所以我很惊讶编译器无法运行它(供您参考:http:// www .cplusplus.com /参考/正则表达式/ match_replace /)
So I thought I'd just run the example they provided for the function:
所以我想我只是运行他们为函数提供的示例:
// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main ()
{
std::string s ("there is a subsequence in the string\n");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
// using string/c-string (3) version:
std::cout << std::regex_replace (s,e,"sub-$2");
// using range/c-string (6) version:
std::string result;
std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
std::cout << result;
// with flags:
std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
std::cout << std::endl;
return 0;
}
But when I run this I get the exact same error!
但是当我运行这个时,我得到完全相同的错误!
Working example: http://ideone.com/yEpgXy
工作示例:http://ideone.com/yEpgXy
So either ideone.com or cplusplus.com are wrong. Rather than bang my head against the wall trying to diagnose the errors of those far wiser than me I'm going to spare my sanity and ask.
所以ideone.com或cplusplus.com都是错的。而不是把我的头撞在墙上试图诊断那些比我更聪明的人的错误,我将不遗余力地问我。
2 个解决方案
#1
7
You need to update your compiler to GCC 4.9.
您需要将编译器更新为GCC 4.9。
Try using boosts regex as an alternative
尝试使用boosts regex作为替代方案
#2
0
Simple code C++ regex_replace only alphanumeric characters
简单代码C ++ regex_replace只有字母数字字符
#include <iostream>
#include <regex>
using namespace std;
int main() {
const std::regex pattern("[^a-zA-Z0-9.-_]");
std::string String = "!#!e-ma.il@boomer.zx";
// std::regex_constants::icase
// Only first
// std::string newtext = std::regex_replace( String, pattern, "X", std::regex_constants::format_first_only );
// All case insensitive
std::string newtext = std::regex_replace( String, pattern, "", std::regex_constants::icase);
std::cout << newtext << std::endl;
return 0;
}
#1
7
You need to update your compiler to GCC 4.9.
您需要将编译器更新为GCC 4.9。
Try using boosts regex as an alternative
尝试使用boosts regex作为替代方案
#2
0
Simple code C++ regex_replace only alphanumeric characters
简单代码C ++ regex_replace只有字母数字字符
#include <iostream>
#include <regex>
using namespace std;
int main() {
const std::regex pattern("[^a-zA-Z0-9.-_]");
std::string String = "!#!e-ma.il@boomer.zx";
// std::regex_constants::icase
// Only first
// std::string newtext = std::regex_replace( String, pattern, "X", std::regex_constants::format_first_only );
// All case insensitive
std::string newtext = std::regex_replace( String, pattern, "", std::regex_constants::icase);
std::cout << newtext << std::endl;
return 0;
}