You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
805 B
37 lines
805 B
#![allow(dead_code)]
|
|
|
|
mod re;
|
|
mod text;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>>
|
|
{
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
if args.len() < 4 {
|
|
println!("Usage: {} <str> <regex> <group>", args[0]);
|
|
println!("Pass `-' as `<str>' to read lines from stdin");
|
|
std::process::exit(1);
|
|
} else {
|
|
let re = re::Regex::compile(&args[2])?;
|
|
let text = &args[1];
|
|
let group: usize = args[3].parse().expect("Invalid group number.");
|
|
|
|
if text == "-" {
|
|
text::stdin_lines(|text| -> Result<bool, re::Error> {
|
|
match re.exec(&text)? {
|
|
Some(g) if g.len() > group => println!("{}", &g[group]),
|
|
_ => (),
|
|
}
|
|
Ok(true)
|
|
})?;
|
|
} else {
|
|
|
|
match re.exec(&text)? {
|
|
Some(g) if g.len() > group => println!("{}", &g[group]),
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|