Skip to content

Instantly share code, notes, and snippets.

@fairjm
Last active April 11, 2022 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fairjm/3a3c963598b17b44cb766afc18960933 to your computer and use it in GitHub Desktop.
Save fairjm/3a3c963598b17b44cb766afc18960933 to your computer and use it in GitHub Desktop.
remove regex matched text in texts of a dir
//! need regex = "1.5"
use regex::Regex;
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
// default is to remove header(better reading for some htmls exported by singleFile, like oreilly, etc)
let mut reg_str= r"<header.*>(.|\n)*?</header>";
let args :Vec<String> = env::args().collect();
let dir;
if args.len() <= 1 {
// default dir is current dir
dir = ".".to_owned();
} else if args.len() <= 2 {
dir = args[1].clone();
} else {
dir = args[1].clone();
reg_str = &args[2];
}
let regex = Regex::new(reg_str).unwrap();
for entry in fs::read_dir(&dir).unwrap() {
let e = entry.unwrap();
if !e.file_type().unwrap().is_file() {
continue;
}
let path = e.path();
let content = fs::read_to_string(&path).unwrap();
let replaced = regex.replace(&content, "").to_string();
let mut new_path = PathBuf::from(&dir);
new_path.push("replaced");
// ignore
let _ = fs::create_dir(new_path.to_str().unwrap());
new_path.push(e.file_name());
println!("{path:?} ---> {new_path:?}");
fs::write(new_path, replaced).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment