使用正则表达式从文件中删除行注释
remove line comment from file by using regex
我使用下面的模式从文件中删除注释行,并在Visual Studio Editor中继承它。但是,相同的模式不适用于C++正则表达式类。
std::regex pattern ("#.*n");
fullText = std::regex_replace (fullText,pattern,"");
上面的代码是实现中非常简短的部分:您可以假设所有文本都一次读入fullText
。
实际结果必须从文件/字符串中删除所有注释行。可以忽略尾随注释。
示例文件.txt扩展名,具有以下文本:
# Initialization file..
# This file supports line comments, and does not support trailing comments.
# Text here is not case sensitive.
# White spaces are ignored in file processing.
# Values are comma "," separated.
Colmn, Colmn,
1, 0xFF,
2, 0xFF,
3, 0xFF,
4, 0xFF,
5, 0xFF,
我假设所有行都必须用n
完成,并且我尝试选择#
和n
之间的所有文本。
提前感谢您的任何建议。
这里的重点是.
与符合 ECMAScript 5 的正则表达式中的回车符不匹配,并且n
模式与 CR 字符不匹配,而r
匹配。
您可以通过在模式末尾使用[rn]*
来解决此问题:
std::regex pattern{"#.*[rn]*"};
相关文章:
- 如何在我从C++文件中读取后立即删除行
- VSCode 中带有 C/C++ 扩展名的多行注释缩进错误
- 如何阻止 ReSharper 在 C++ 中格式化多行注释
- 从 xtensor 中的视图中删除行
- 删除被注释掉的代码,而不是实际的赞美
- 从 TXT 文件中删除行不起作用
- 使用正则表达式从文件中删除行注释
- QSyntax荧光笔和多行注释
- CLion 行注释的不一致自动缩进
- 从文件MQL4删除行
- 使用自定义模型从Qtreeview删除行
- 提升信息解析器在读取/写入 INFO 格式文件时删除所有注释
- 分段错误从矩阵中删除行
- gcc多行注释警告
- Qt表小部件,按钮删除行
- 2D矢量-通过搜索删除行
- Armadillo-在立方体中删除行
- visualstudio继续多行注释
- c++从文本文件中删除行(需要解释)
- 从简历垫中删除行或列的最佳方法是什么