我正在使用 std::regex_replace 来修改字符串。我既需要限制替换,又需要完成替换的数量。
我使用了以下代码:
std::wregex rx(pattern);
size_t n = 0; // one match
size_t ns = 3;
wstring result = src;
wstring old; // old value
bool replaced = false;
do {
old = result;
result = std::regex_replace(result, rx, replace, std::regex_constants::format_first_only);
replaced = result != old;
if (replaced)
n++;
} while (replaced && n < ns);
它工作正常,我既可以限制替代品的数量,又可以获取它们的数量。但是这段代码从头开始分析字符串,因此,如果我有以下值:
"banana" for src, "(an)" for pattern and "$1-" for replace
它产生以下输出:ban---ana 而不是 ban-an-a。显然,这是因为 std::regex_replace 从头开始分析字符串。一种解决方案可能是使用迭代器来定义要分析的第一个字符,但在这种情况下,我需要获取指向被替换字符之后的字符的迭代器,但我怎样才能得到它?
最佳答案
事实证明,这比我想象的要棘手。我没有发现任何 std::regex_replace()
函数在这里非常有用。
我决定采用直接的 std::wsregex_iterator
解决方案,该解决方案基于此处实现描述中建议的算法:
http://en.cppreference.com/w/cpp/regex/regex_replace
这是我想出的:
#include <regex>
#include <string>
#include <iterator>
#include <iostream>
int main()
{
std::size_t ns = 3;
std::wstring text = L"banana";
std::wstring pattern = L"(an)";
std::wstring replace = L"$1-";
std::wstring result;
std::wregex rx(pattern);
auto iter = std::wsregex_iterator(text.begin(), text.end(), rx);
auto stop = std::wsregex_iterator();
auto last_iter = iter;
auto out = std::back_inserter(result);
for(std::size_t n = ns; n-- && iter != stop; ++iter)
{
out = std::copy(iter->prefix().first, iter->prefix().second, out);
out = iter->format(out, replace);
last_iter = iter;
}
out = std::copy(last_iter->suffix().first, last_iter->suffix().second, out);
std::wcout << " text: " << text << '\n';
std::wcout << "result: " << result << '\n';
}
输出:
text: banana
result: ban-an-a
关于c++ - std::regex_replace 替换 n 次出现并获取替换次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680520/