c++ - 有条件的 RegEx 替换

标签 c++ regex windows

我有一个字符串:

std::string String = "<!\\[LOG\\[somestringhere\\]LOG\\]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponenet\" context=\"\" type=\"1\" thread=\"0\" file=\"mxyfile.cpp\"><!\\[LOG\\[somestringhere\\]LOG\\]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponenet\" context=\"\" type=\"1\" thread=\"0\" file=\"mxyfile.cpp\">";

我想插入一个 \n字符在这里 ><![LOG[>之后签名。

到目前为止我的代码:

#include <regex>

const std::tr1::regex pattern( "(>|\")<!\\[LOG\\[" );
std::string replace = ">\n<![LOG[";
std::string newtext = std::tr1::regex_replace( String, pattern, replace );
std::cout << newtext << std::endl;

这很好用,但不幸的是有一个小问题。并非每一行都以 > 结尾.在某些情况下,会保留 \"<!\\[LOG\\[而不是 ><!\\[LOG\\[应该如此。

如果最后>缺少则结果将是 "\n<![LOG[而不是 >\n<![LOG[应该如此。

所以我的问题是,解决这个问题最简单/最好的方法是什么? 我是否应该以某种方式检查模式是否存在,然后相应地设置替换字符串?

希望我想要的是可以理解的。

谢谢。

更新:
抱歉,但正如我所见,我在字符串的外观上犯了一个错误,这引起了一些误解。 日志文件中的字符串(我将日志文件读入 std::string 并对其进行处理)如下。这实际上是两行,但缺少换行符,这就是我要插入的内容。

案例一:
字符串看起来像这样:
<![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp"><![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp">

由此我想得到的结果是:
<![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp">**LineBreakHere** <![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp">

请注意换行的位置。

案例二: 字符串大致如下:
<![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp"<![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp"

请注意,有一个 >file="myfile.cpp" 之后丢失

如果是这样的话,我想得到和以前一样的结果:
<![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp">**LineBreakHere and the missing > was also inserted** <![LOG[somestring]LOG]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponent" context="" type="1" thread="0" file="myfile.cpp"> **also inserted missing >**

所以基本上,我想插入一个换行符,如果缺少 >如果可能的话,我也想插入它。

最佳答案

你的正则表达式应该是这样的

"(>|\")<!\\\\\\[LOG\\\\\\["

\ 的 4 个斜杠和 2 用于转义方括号。编写正则表达式的更好方法是使用 R"(...)"符号(“原始字符串文字”):

const std::regex pattern( R"((>|\")<!\\\[LOG\\\[)" );

代码将是:

const std::regex pattern( R"((>|\")<!\\\[LOG\\\[)" );
std::string String = "<!\\[LOG\\[somestringhere\\]LOG\\]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponenet\" context=\"\" type=\"1\" thread=\"0\" file=\"mxyfile.cpp\"><!\\[LOG\\[somestringhere\\]LOG\\]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponenet\" context=\"\" type=\"1\" thread=\"0\" file=\"mxyfile.cpp\">";
std::string replace = "$1\n<![LOG[";
std::string newtext = std::regex_replace( String, pattern, replace );
std::cout << newtext << std::endl;

nextext

<!\[LOG\[somestringhere\]LOG\]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponenet" context="" type="1" thread="0" file="mxyfile.cpp">
<![LOG[somestringhere\]LOG\]!><time="12:34:30.0+120" date="9-14-2015" component="mycomponenet" context="" type="1" thread="0" file="mxyfile.cpp">

请注意,替换字符串现在包含反向引用 $1到第一个捕获的组(与括号内的子模式匹配的组 (<|\") ,我们在替换中安全地恢复它。这就是我与反斜杠一起修改的全部内容。

IDEONE demo

A regex demo

更新:

您可以使用 R"((<!\[LOG\[[\s\S]*?\]!><[^<]*)(\">?))"正则表达式:

const std::regex pattern( R"((<!\[LOG\[[\s\S]*?\]!><[^<]*)(\">?))" );
std::string String = "<![LOG[somestring]LOG]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponent\" context=\"\" type=\"1\" thread=\"0\" file=\"myfile.cpp\"<![LOG[somestring]LOG]!><time=\"12:34:30.0+120\" date=\"9-14-2015\" component=\"mycomponent\" context=\"\" type=\"1\" thread=\"0\" file=\"myfile.cpp\"";
std::string replace = "$1\">\n";
std::string newtext = std::regex_replace( String, pattern, replace );
std::cout << newtext << std::endl;

Ideone demo

正则表达式解释:

该模式有 2 个捕获组:一个捕获 <![LOG[ 的开头直到下一个节点( (<!\[LOG\[[\s\S]*?\]!><[^<]*) )的末尾,另一个节点捕获带有右尖括号的引号或仅引号 (">|") .

  • (<!\[LOG\[ - 匹配 <![LOG[字面上(第一个捕获组的开始)
  • [\s\S]*? - 匹配 0 个或多个任意字符(甚至是换行符)
  • \]!>< - 匹配 ]!><从字面上看
  • [^<]*) - 匹配除 < 以外的 0 个或多个字符(第一个捕获组结束)
  • (\">|\") - 匹配和捕获 ">" .你可以写成 (\">?) .

关于c++ - 有条件的 RegEx 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652720/

相关文章:

c++ - 稀疏矩阵乘法的最佳包

python - 如何找到更改目录名称的路径?

C++ 在控制台应用程序中播放视频音频

python - 在 Windows 上安装 Airflow

c++ - 清空 Win32 弹出式菜单

c++ - VS C++ 程序仅在从文件夹运行 .exe 时才有效? [不是VS调试]

c++ - size_t 在 C++ 中转换/转换为字符串

c++ - 快速稀疏矩阵乘法

regex - sed 替换部分字符串

c# - 固定长度的正则表达式数字模式