[英]remove line comment from file by using regex
I used the pattern below to remove comment lines from file, and succeed it in Visual Studio Editor.我使用下面的模式从文件中删除注释行,并在 Visual Studio 编辑器中成功。 However, the same pattern didn't work with C++ regex class.
但是,相同的模式不适用于 C++ 正则表达式 class。
std::regex pattern ("#.*\n");
fullText = std::regex_replace (fullText,pattern,"");
Code above is a very brief part from the implementation: You can assume all text is read into fullText
at once.上面的代码是实现的一个非常简短的部分:您可以假设所有文本都一次读入
fullText
。
Actual results must remove all comment lines from file/string.实际结果必须从文件/字符串中删除所有注释行。 Trailing comments can be ignored.
尾随注释可以忽略。
Sample file is.txt extension and has the text below:示例文件为.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,
I assume all lines must be finished with \n
, and I attempt to select all text between #
and \n
.我假设所有行都必须以
\n
结尾,并且我尝试 select #
和\n
之间的所有文本。
Thanks in advance for the any advice.提前感谢您的任何建议。
The point here is that .
这里的重点是
.
does not match carriage returns in the ECMAScript 5 compliant regex and \n
pattern does not match CR chars while \r
does.与 ECMAScript 5 兼容正则表达式中的回车不匹配,并且
\n
模式与 CR 字符不匹配,而\r
匹配。
You may fix the issue by using [\r\n]*
at the end of the pattern:您可以通过在模式末尾使用
[\r\n]*
来解决此问题:
std::regex pattern{"#.*[\r\n]*"};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.