[英]remove line comment from file by using regex
我使用下面的模式從文件中刪除注釋行,並在 Visual Studio 編輯器中成功。 但是,相同的模式不適用於 C++ 正則表達式 class。
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
結尾,並且我嘗試 select #
和\n
之間的所有文本。
提前感謝您的任何建議。
這里的重點是.
與 ECMAScript 5 兼容正則表達式中的回車不匹配,並且\n
模式與 CR 字符不匹配,而\r
匹配。
您可以通過在模式末尾使用[\r\n]*
來解決此問題:
std::regex pattern{"#.*[\r\n]*"};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.