gpt4 book ai didi

c++ - regex_match 期间的 error_stack

转载 作者:行者123 更新时间:2023-11-30 04:44:50 25 4
gpt4 key购买 nike

目标是:这个 json:

{"secretWord1":"private", "something": "\"secretWord2\":\"privateToo\""}

通过 regex_match 转换为:

{"secretWord1":"****", "something": "\"secretWord2\":\"****\""}

我有一个包含三个正则表达式的代码:

std::regex regex1(R"~((\\\"|")((?:[^\\"]*)(?:secretWord1|secretWord2))\1:\1([^\\"]*)\1)~", std::regex_constants::icase);
std::regex regex2(R"~((\\\")((?:[^\\"]*)(?:secretWord1|secretWord2))\1:\1([^\\"]*)\1)~", std::regex_constants::icase);
std::regex regex3(R"~((")((?:[^\\"]*)(?:secretWord1|secretWord2))\1:\1([^\\"]*)\1)~", std::regex_constants::icase);

std::string replaced = someJsonData;
replaced = std::regex_replace(replaced, regex1, "$1$2$1:$1****$1");
replaced = std::regex_replace(std::regex_replace(replaced, regex2, "$1$2$1:$1****$1"), regex3, "$1$2$1:$1****$1");

我想替换 secret 信息并将其隐藏在星星后面。第一个正则表达式在

上失败
error_stack: regex_error(error_stack): There was insufficient memory to determine whether the regular expression could match the specified character sequence.

第一个表达式有问题吗?因为其他两个表达式只是相互补充,最终,它完成与 regex1 相同的工作,但当我运行它们时它们运行良好。

我无法在它失败时提供示例代码,但文件不是很大(大约 30kB)。当我尝试使用 JSON 生成器时,regex1 明显比结合使用 regex2+regex3 慢。

最佳答案

我不太了解 C++ 和内存问题,但这似乎非常匹配:

(\\?"(secretword1|secretword2)\\?":\\?")(.*?)(\\?")

https://regex101.com/r/T8pY0V/2


请注意,我强烈建议获取一个 JSON 库,但此正则表达式可能会在紧要关头起作用。您需要找出所有失败的边缘情况。

关于c++ - regex_match 期间的 error_stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57558910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com