Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用不同的数字替换每个正则表达式匹配项_C++_Regex - Fatal编程技术网

C++ 用不同的数字替换每个正则表达式匹配项

C++ 用不同的数字替换每个正则表达式匹配项,c++,regex,C++,Regex,我做作业有点问题。 我需要制作一个分数方程解析器,其中分数由a | b表示。 我将字符串中的所有分数转换成一个向量,现在我想用后面的数字替换字符串中的分数,这样我就可以更轻松地将它们放入调车场算法的堆栈中。 例如: 更改此字符串 2 | 1+4 | 1*5 | 2-18 | 5 到 1+2*3-4 如果我这样做了,我可以通过等式中的数字调用我想要的分数,这就是索引。 将其从中缀改为后缀会更容易。 问题是我真的不知道如何用不同的数字替换这些分数。 我的正则表达式是:regex reg(\\d+\\

我做作业有点问题。 我需要制作一个分数方程解析器,其中分数由a | b表示。 我将字符串中的所有分数转换成一个向量,现在我想用后面的数字替换字符串中的分数,这样我就可以更轻松地将它们放入调车场算法的堆栈中。
例如:
更改此字符串
2 | 1+4 | 1*5 | 2-18 | 5


1+2*3-4

如果我这样做了,我可以通过等式中的数字调用我想要的分数,这就是索引。 将其从中缀改为后缀会更容易。
问题是我真的不知道如何用不同的数字替换这些分数。
我的正则表达式是:
regex reg(\\d+\\\\124;\\ d+)

这匹配字符串中的每个分数,现在是我不知道如何继续的部分。
使用
regex\u replace()
将用相同的内容替换所有匹配项。
我非常希望得到一些帮助。

您可以使用,并用替换的数字生成一个新字符串:

const std::string s = "2|1+4|1*5|2-18|5";

std::regex r("\\d+\\|\\d+");
// std::regex r(R"(\d+\|\d+)");  // as a raw-string literal, which reduces the need for backslashes

auto begin = std::sregex_iterator(s.begin(), s.end(), r);
auto end = std::sregex_iterator();

int count = 1;
std::string result;
for (std::sregex_iterator i = begin; i != end; ++i) 
    result += i->prefix().str() + std::to_string(count++);

std::cout << result << '\n'; // prints 1+2*3-4
const std::string s=“2 | 1+4 | 1*5 | 2-18 | 5”;
标准::正则表达式r(“\\d+\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\d+”);
//std::regex r(r“(\d+\\d+));//作为原始字符串文字,这减少了反斜杠的需要
auto begin=std::sregex_迭代器(s.begin(),s.end(),r);
auto end=std::sregex_迭代器();
整数计数=1;
std::字符串结果;
for(std::sregx_迭代器i=begin;i!=end;++i)
结果+=i->prefix().str()+std::to_字符串(count++);

有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题了。值得注意的是,原始字符串有帮助,特别是在正则表达式中:
R”(\d+\\124;\ d+)