Last active
February 16, 2017 23:50
-
-
Save naviarh/608133c8f51d312ce8b1a2e75691c62f to your computer and use it in GitHub Desktop.
Подсчёт совпадений в файле по регулярному выражению
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Вариант байтного построчного чтения | |
extern crate regex; | |
use regex::bytes::Regex; | |
use std::fs::File; | |
use std::io::BufReader; | |
use std::io::prelude::*; | |
fn main() { | |
println!(); | |
println!("Подсчёт совпадений регулярки"); | |
println!("регулярка: #"); | |
let file = "/etc/profile"; // - файл | |
println!("фвйл: {}", file); | |
let mut matches: i32 = 0; | |
let open = File::open(file); | |
if open.is_err() { | |
println!("Ошибка чтения файла"); | |
} | |
let mut open = BufReader::new(open.unwrap()); | |
let rgx = Regex::new(r"#").unwrap(); // - регулярка | |
let mut buf = Vec::with_capacity(4096); | |
while let Ok(x) = open.read_until(b'\n', &mut buf) { | |
if x == 0 { break; } | |
if rgx.is_match(&buf) { | |
matches = matches + 1; | |
} | |
buf.clear(); | |
} | |
println!(); | |
println!("----------------------------------"); | |
println!(); | |
println!("Количество совпадений: {}", matches); | |
println!(); | |
println!(); | |
println!(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Вариант построчного чтения с буфера файла | |
extern crate regex; | |
use regex::Regex; | |
use std::fs::File; | |
use std::io::BufReader; | |
use std::io::prelude::*; | |
fn main() { | |
println!(); | |
println!("Подсчёт совпадений регулярки"); | |
println!("регулярка: #"); | |
let file = "/etc/profile"; // - файл | |
println!("фвйл: {}", file); | |
let mut matches: i32 = 0; | |
let open = File::open(file); | |
if open.is_err() { | |
println!("Ошибка чтения файла"); | |
} | |
let open = BufReader::new(open.unwrap()); | |
let rgx = Regex::new(r"#").unwrap(); // - регулярка | |
for line in open.lines() { | |
if rgx.is_match(line.unwrap().as_str()) { | |
matches = matches + 1; | |
} | |
} | |
println!(); | |
println!("----------------------------------"); | |
println!(); | |
println!("Количество совпадений: {}", matches); | |
println!(); | |
println!(); | |
println!(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Вариант чтения файла целиком | |
#[macro_use] extern crate lazy_static; | |
extern crate regex; | |
use regex::Regex; | |
use std::fs::File; | |
use std::io::Read; | |
// другой способ, щас ненужен ---\ | |
// вызов: match_static(text) // -> Bool | |
fn match_static(text: &str) -> bool { | |
lazy_static! { | |
static ref RE: Regex = Regex::new(r"/").unwrap(); | |
} | |
RE.is_match(text) | |
} | |
// -----------------------------/ | |
fn main() { | |
println!(); | |
println!("Подсчёт совпадений регулярки"); | |
println!("регулярка: #"); | |
let file = "/etc/profile"; // - файл | |
println!("фвйл: {}", file); | |
let mut matches: i32 = 0; | |
let mut text = String::new(); | |
let open = File::open(file); | |
if open.is_err() { | |
println!("Ошибка чтения файла"); | |
} | |
open.unwrap().read_to_string(&mut text); | |
let strings: Vec<&str> = text.split("\n").collect(); | |
let rgx = Regex::new(r"#").unwrap(); // - регулярка | |
for string in strings { | |
if rgx.is_match(string) { | |
matches = matches + 1; | |
} | |
} | |
println!(); | |
println!("----------------------------------"); | |
println!(); | |
println!("Количество совпадений: {}", matches); | |
println!(); | |
println!(); | |
println!(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Создать: cargo
файле Cargo.toml надо прописать:
[dependencies]
regex = ""
# для match_static() так же:
lazy_static = ""