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

4 years ago
#![allow(dead_code)]
4 years ago
mod re;
mod text;
4 years ago
4 years ago
fn main() -> Result<(), Box<dyn std::error::Error>>
{
4 years ago
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
println!("Usage: {} <str> <regex> <group>", args[0]);
4 years ago
println!("Pass `-' as `<str>' to read lines from stdin");
std::process::exit(1);
4 years ago
} else {
4 years ago
let re = re::Regex::compile(&args[2])?;
4 years ago
let text = &args[1];
let group: usize = args[3].parse().expect("Invalid group number.");
4 years ago
if text == "-" {
text::stdin_lines(|text| -> Result<bool, re::Error> {
match re.exec(&text)? {
Some(g) if g.len() > group => println!("{}", &g[group]),
_ => (),
}
4 years ago
Ok(true)
})?;
} else {
4 years ago
match re.exec(&text)? {
Some(g) if g.len() > group => println!("{}", &g[group]),
_ => (),
}
}
}
4 years ago
Ok(())
}