Skip to content

Instantly share code, notes, and snippets.

@naviarh
Last active February 16, 2017 23:50
Show Gist options
  • Save naviarh/608133c8f51d312ce8b1a2e75691c62f to your computer and use it in GitHub Desktop.
Save naviarh/608133c8f51d312ce8b1a2e75691c62f to your computer and use it in GitHub Desktop.
Подсчёт совпадений в файле по регулярному выражению
// Вариант байтного построчного чтения
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!();
}
// Вариант построчного чтения с буфера файла
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!();
}
// Вариант чтения файла целиком
#[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!();
}
@naviarh
Copy link
Author

naviarh commented Feb 16, 2017

Создать: cargo
файле Cargo.toml надо прописать:

[dependencies]
regex = ""
# для match_static() так же:
lazy_static = "
"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment