use std::fs;
use regex::Regex;
fn main() {
// 从控制台获取文件夹目录
// let mut input = String::new();
// println!("请输入文件夹目录:\t");
// std::io::stdin().read_line(&mut input).expect("read_line error!");
let mut input = String::from(r"C:\Users\30456\Desktop\软著\BLUE\Bar.cpp");
// 读取cpp文件内容
let content = fs::read_to_string(&mut input).expect("Failed to read file");
// 删除注释和空行
let modified_content = remove_comments_and_empty_lines(content);
// 将修改后的内容写回cpp文件
fs::write(&mut input, modified_content).expect("Failed to write file");
}
fn remove_comments_and_empty_lines(content: String) -> String {
// 正则表达式匹配注释和空行
let re = Regex::new(r"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|//.*|[\t ]*\n").unwrap();
let modified_content = re.replace_all(&content, "");
modified_content.into_owned()
}
【Rust基础】删除指定cpp文件的所有注释和空行
最新推荐文章于 2024-06-29 15:12:18 发布