鱼C论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: janeyjhon

[已解决]怎样让字符串迭代替换剩下的

[复制链接]
 楼主| 发表于 6 天前 | 显示全部楼层
FishC 发表于 2024-10-13 18:14
抱歉为你带来了困惑。你可以尝试使用如下算法,保证一次只替换一个匹配项:

std::sregex_iterator end;这个变量一直没初始化呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 6 天前 | 显示全部楼层
janeyjhon 发表于 2024-10-17 10:27
std::sregex_iterator end;这个变量一直没初始化呢


在C++中,使用`std::sregex_iterator`来遍历正则表达式的匹配结果时,通常用默认构造的`std::sregex_iterator`来代表结束迭代器,即`end`变量是默认初始化的,它本身就代表了"没有更多匹配项"的状态。因此,不需要对`end`进行显式初始化。

下面是修改后的完整代码,确保每个匹配项逐个替换为递增的数字:
#include <iostream>
#include <regex>
#include <string>

std::string replace_and_increment(std::string input, std::regex pattern)
{
    std::string result = input;
    std::sregex_iterator it(input.begin(), input.end(), pattern);
    std::sregex_iterator end; // 默认构造,用于比较结束

    while (it != end)
    {
        std::smatch match = *it;
        std::string matchStr = match.str();

        // 去掉括号得到数字字符串
        std::string numStr = matchStr.substr(1, matchStr.length() - 2);
        int num = std::stoi(numStr);

        // 生成替换后的字符串
        std::string replacement = "(" + std::to_string(num + 1) + ")";

        // 在result中替换匹配项
        result.replace(match.position(), match.length(), replacement);

        // 用新result字符串刷新迭代器
        it = std::sregex_iterator(result.begin(), result.end(), pattern);
        ++it; // 移动到下一个匹配项
    }

    return result;
}

int main()
{
    std::string input = "\n(2)333\n(3)444";
    std::regex pattern(R"(\(\d+\))");
    
    std::string result = replace_and_increment(input, pattern);
    std::cout << result << std::endl; // 期望输出: \n(3)333\n(4)444

    return 0;
}

在这段代码中,我们通过每次替换后重新初始化迭代器以继续遍历新字符串,从而达到替换每个匹配项的目的。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 6 天前 | 显示全部楼层    本楼为最佳答案   


抱歉之前的回答可能有些不准确,`std::regex_replace` 不直接支持以 `lambda` 捕获的状态进行逐个替换。由于 `std::regex_replace` 没有原生接受 `lambda` 参数的重载,因此我们需要手动管理迭代状态,逐个替换匹配项。在 C++ 中复用正则表达式的捕获功能,可以通过以下迭代的方法逐步替换字符串中的内容:
#include <iostream>
#include <string>
#include <regex>

std::string replace_and_increment(std::string input, std::regex pattern) {
    std::string result;
    std::sregex_iterator iter(input.begin(), input.end(), pattern);
    std::sregex_iterator end;

    size_t lastPos = 0; // 用于记录上一个匹配结束的位置

    while (iter != end) {
        std::smatch match = *iter;
        
        // 处理并追加在上一次匹配与当前匹配之间的内容
        result += input.substr(lastPos, match.position() - lastPos);

        // 提取匹配的字符串去掉括号
        std::string numStr = match.str().substr(1, match.str().length() - 2);
        int num = std::stoi(numStr) + 1;

        // 替换为新的值
        result += "(" + std::to_string(num) + ")";

        // 更新 lastPos
        lastPos = match.position() + match.length();
        
        ++iter;
    }

    // 追加最后一个匹配后面的内容
    result += input.substr(lastPos);
    
    return result;
}

int main() {
    std::string input = "\n(2)333\n(3)444";
    std::regex pattern(R"(\(\d+\))");

    std::string result = replace_and_increment(input, pattern);
    std::cout << result << std::endl;

    return 0;
}

这个代码在逐个调整匹配项时:
- 使用 `std::sregex_iterator` 遍历正则表达式找到的所有匹配项。
- 利用 `std::string::substr` 和 `std::string::append` 手动构建替换后的结果字符串。
- 只针对每个匹配项独立地进行替换,并确保后续处理不受之前替换的影响。这符合您对逐个替换顺序递增的需求。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
std::string result = input;
std::sregex_iterator it(input.begin(), input.end(), pattern);
std::sregex_iterator end;

while (it != end)
{
        std::smatch match = *it;
        std::string matchStr = match.str();

        // 提取并去掉圆括号
        std::string numStr = matchStr.substr(2, matchStr.length() - 3);
        int num = std::stoi(numStr);

        // 生成替换后的字符串
        std::string replacement = "(" + std::to_string(num + 1) + ")";

        // 逐个替换匹配项
        result.replace(match.position(), match.length(), replacement);

        // 更新迭代器,重新匹配
        it = std::sregex_iterator(result.begin(), result.end(), pattern);
        ++end; // 确保不会无限循环
}

return result;
这一段代码我改改或许可以,但是std::sregex_iterator end;一直没有初始值内存溢出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 6 天前 | 显示全部楼层
janeyjhon 发表于 2024-10-17 10:35
std::string result = input;
std::sregex_iterator  ...


在这段代码中,我们通常使用默认构造的 `std::sregex_iterator` 来代表结束状态的迭代器,即它自身就是结束状态,所以不需要显式地进行初始化。这里的 `end` 本身用于表示匹配结束。

不过,这段代码中出现了使用不当的地方:这里用到了 `++end;`,这样没有意义且可能会导致意图不明的问题。你无需操作 `end` 迭代器,只需通过它判断是否达到了遍历的末尾。

请改为如下操作:
#include <iostream>
#include <regex>
#include <string>

std::string replace_and_increment(std::string input, std::regex pattern) {
    std::string result = input;
    std::sregex_iterator it(input.begin(), input.end(), pattern);
    std::sregex_iterator end;

    while (it != end) {
        std::smatch match = *it;
        std::string matchStr = match.str();

        // 提取并去掉圆括号
        std::string numStr = matchStr.substr(1, matchStr.length() - 2);
        int num = std::stoi(numStr);

        // 生成替换后的字符串
        std::string replacement = "(" + std::to_string(num + 1) + ")";

        // 逐个替换匹配项
        result.replace(match.position(), match.length(), replacement);

        // 更新迭代器,使其指向替换之后的新字符串
        it = std::sregex_iterator(result.begin() + match.position() + replacement.length(), result.end(), pattern);
    }

    return result;
}

int main() {
    std::string input = "\n(2)333\n(3)444";
    std::regex pattern(R"(\(\d+\))");

    std::string result = replace_and_increment(input, pattern);
    std::cout << result << std::endl;

    return 0;
}

在这个改进后的版本中:

- 我们用到了一个包含匹配结束状态的迭代器 `end` 来判断循环是否结束,但不对它进行增减操作(去掉了 `++end;` 这一行)。
- 按照匹配更新后的字符串,结合 `match.position()` 来确定下一个匹配的开始位置,以避免多次替换同一部分。这样当某个部分修改后,可以继续在更新的字符串起点之后进行搜索和替换。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
已经解决了,谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 6 天前 | 显示全部楼层
janeyjhon 发表于 2024-10-17 17:57
已经解决了,谢谢!


不客气!很高兴能够帮助你解决问题。如果你还有其他问题或者需要进一步的帮助,欢迎随时提问。祝你编程顺利!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-23 02:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表