gpt4 book ai didi

c++ - 通过正则表达式崩溃删除 RTF 标签

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:02:30 24 4
gpt4 key购买 nike

我正在使用 C++ VS 2008 去除 RTF 标签以仅在 RTF 文本中进行搜索。

我从 CRichTextEdit 获取到 CString 的 RTF 文本工作正常。下面是我如何尝试删除标签:

std::tr1::cmatch res;
std::string str = note;
const std::tr1::regex rx("({\\)(.+?)(})|(\\)(.+?)(\b)");
std::string replacement = "";
std::string result = std::tr1::regex_replace(str,rx,replacement);

CString strSearchText = result.c_str();

第 3 行在上面的模式中崩溃。一个简单的文本替换模式工作得很好:Ex 将 “fast fox” 中的 “fast” 替换为空字符串。崩溃信息在这里:

First-chance exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: std::tr1::regex_error at memory location 0x0012ee20..
First-chance exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: std::tr1::regex_error at memory location 0x0012ee20..

最佳答案

您需要对反斜杠进行转义,因为它们在正则表达式和 C++ 字符串文字中都用作转义字符。这意味着要匹配单个反斜杠,您需要四个反斜杠字符。我认为这应该有效:"({\\\\)(.+?)(})|(\\\\)(.+?)(\\b)"

这应该可以修复异常,但如果它仍然发生,您可以捕获它并使用异常对象的 what() 或 code() 方法来查看问题所在。

编辑:大括号也应该被转义,因为它们是正则表达式中的特殊字符。所以使用:

"(\\{\\\\)(.+?)(\\})|(\\\\)(.+?)(\\b)"

关于c++ - 通过正则表达式崩溃删除 RTF 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794036/

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